Debian Codegraph @

closure-library/closure/goog/debug/logrecordserializer.js

raw
1 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Static methods for serializing and deserializing log
17 * messages. These methods are deliberately kept separate from logrecord.js
18 * and logger.js because they add dependencies on goog.json and goog.object.
19 *
20 */
21
22 goog.provide('goog.debug.logRecordSerializer');
23
24 goog.require('goog.debug.LogRecord');
25 goog.require('goog.debug.Logger');
26 goog.require('goog.json');
27 goog.require('goog.object');
28
29
30 /**
31 * Enumeration of object keys used when serializing a log message.
32 * @enum {string}
33 * @private
34 */
35 goog.debug.logRecordSerializer.Param_ = {
36 TIME: 't',
37 LEVEL_NAME: 'ln',
38 LEVEL_VALUE: 'lv',
39 MSG: 'm',
40 LOGGER_NAME: 'n',
41 SEQUENCE_NUMBER: 's',
42 EXCEPTION: 'e'
43 };
44
45
46 /**
47 * Serializes a LogRecord to a JSON string. Note that any associated
48 * exception is likely to be lost.
49 * @param {goog.debug.LogRecord} record The record to serialize.
50 * @return {string} Serialized JSON string of the log message.
51 */
52 goog.debug.logRecordSerializer.serialize = function(record) {
53 var param = goog.debug.logRecordSerializer.Param_;
54 return goog.json.serialize(
55 goog.object.create(
56 param.TIME, record.getMillis(), param.LEVEL_NAME,
57 record.getLevel().name, param.LEVEL_VALUE, record.getLevel().value,
58 param.MSG, record.getMessage(), param.LOGGER_NAME,
59 record.getLoggerName(), param.SEQUENCE_NUMBER,
60 record.getSequenceNumber(), param.EXCEPTION,
61 record.getException() && record.getException().message));
62 };
63
64
65 /**
66 * Deserializes a JSON-serialized LogRecord.
67 * @param {string} s The JSON serialized record.
68 * @return {!goog.debug.LogRecord} The deserialized record.
69 */
70 goog.debug.logRecordSerializer.parse = function(s) {
71 return goog.debug.logRecordSerializer.reconstitute_(
72 /** @type {!Object} */ (JSON.parse(s)));
73 };
74
75
76 /**
77 * Reconstitutes LogRecord from the JSON object.
78 * @param {Object} o The JSON object.
79 * @return {!goog.debug.LogRecord} The reconstituted record.
80 * @private
81 */
82 goog.debug.logRecordSerializer.reconstitute_ = function(o) {
83 var param = goog.debug.logRecordSerializer.Param_;
84 var level = goog.debug.logRecordSerializer.getLevel_(
85 o[param.LEVEL_NAME], o[param.LEVEL_VALUE]);
86
87 var ret = new goog.debug.LogRecord(
88 level, o[param.MSG], o[param.LOGGER_NAME], o[param.TIME],
89 o[param.SEQUENCE_NUMBER]);
90 var exceptionMessage = o[param.EXCEPTION];
91 if (goog.isDefAndNotNull(exceptionMessage)) {
92 ret.setException(new Error(exceptionMessage));
93 }
94 return ret;
95 };
96
97
98 /**
99 * @param {string} name The name of the log level to return.
100 * @param {number} value The numeric value of the log level to return.
101 * @return {!goog.debug.Logger.Level} Returns a goog.debug.Logger.Level with
102 * the specified name and value. If the name and value match a predefined
103 * log level, that instance will be returned, otherwise a new one will be
104 * created.
105 * @private
106 */
107 goog.debug.logRecordSerializer.getLevel_ = function(name, value) {
108 var level = goog.debug.Logger.Level.getPredefinedLevel(name);
109 return level && level.value == value ? level : new goog.debug.Logger.Level(
110 name, value);
111 };