aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/debug/logrecordserializer.js
blob: e387bed1abec8b477f80d7827c21cb6179330935 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2011 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Static methods for serializing and deserializing log
 * messages.  These methods are deliberately kept separate from logrecord.js
 * and logger.js because they add dependencies on goog.json and goog.object.
 *
 */

goog.provide('goog.debug.logRecordSerializer');

goog.require('goog.debug.LogRecord');
goog.require('goog.debug.Logger.Level');
goog.require('goog.json');
goog.require('goog.object');


/**
 * Enumeration of object keys used when serializing a log message.
 * @enum {string}
 * @private
 */
goog.debug.logRecordSerializer.Param_ = {
  TIME: 't',
  LEVEL_NAME: 'ln',
  LEVEL_VALUE: 'lv',
  MSG: 'm',
  LOGGER_NAME: 'n',
  SEQUENCE_NUMBER: 's',
  EXCEPTION: 'e',
  EXCEPTION_TEXT: 'et'
};


/**
 * Serializes a LogRecord to a JSON string.  Note that any associated
 * exception is likely to be lost.
 * @param {goog.debug.LogRecord} record The record to serialize.
 * @return {string} Serialized JSON string of the log message.
 */
goog.debug.logRecordSerializer.serialize = function(record) {
  var param = goog.debug.logRecordSerializer.Param_;
  return goog.json.serialize(goog.object.create(
      param.TIME, record.getMillis(),
      param.LEVEL_NAME, record.getLevel().name,
      param.LEVEL_VALUE, record.getLevel().value,
      param.MSG, record.getMessage(),
      param.LOGGER_NAME, record.getLoggerName(),
      param.SEQUENCE_NUMBER, record.getSequenceNumber(),
      param.EXCEPTION, record.getException(),
      param.EXCEPTION_TEXT, record.getExceptionText()));
};


/**
 * Deserializes a JSON-serialized LogRecord.
 * @param {string} s The JSON serialized record.
 * @return {!goog.debug.LogRecord} The deserialized record.
 */
goog.debug.logRecordSerializer.parse = function(s) {
  return goog.debug.logRecordSerializer.reconstitute_(goog.json.parse(s));
};


/**
 * Deserializes a JSON-serialized LogRecord.  Use this only if you're
 * naive enough to blindly trust any JSON formatted input that comes
 * your way.
 * @param {string} s The JSON serialized record.
 * @return {!goog.debug.LogRecord} The deserialized record.
 */
goog.debug.logRecordSerializer.unsafeParse = function(s) {
  return goog.debug.logRecordSerializer.reconstitute_(goog.json.unsafeParse(s));
};


/**
 * Common reconsitution method for for parse and unsafeParse.
 * @param {Object} o The JSON object.
 * @return {!goog.debug.LogRecord} The reconstituted record.
 * @private
 */
goog.debug.logRecordSerializer.reconstitute_ = function(o) {
  var param = goog.debug.logRecordSerializer.Param_;
  var level = goog.debug.logRecordSerializer.getLevel_(
      o[param.LEVEL_NAME], o[param.LEVEL_VALUE]);

  var ret = new goog.debug.LogRecord(level, o[param.MSG],
      o[param.LOGGER_NAME], o[param.TIME], o[param.SEQUENCE_NUMBER]);
  ret.setException(o[param.EXCEPTION]);
  ret.setExceptionText(o[param.EXCEPTION_TEXT]);
  return ret;
};


/**
 * @param {string} name The name of the log level to return.
 * @param {number} value The numeric value of the log level to return.
 * @return {goog.debug.Logger.Level} Returns a goog.debug.Logger.Level with
 *     the specified name and value.  If the name and value match a predefined
 *     log level, that instance will be returned, otherwise a new one will be
 *     created.
 * @private
 */
goog.debug.logRecordSerializer.getLevel_ = function(name, value) {
  var level = goog.debug.Logger.Level.getPredefinedLevel(name);
  return level && level.value == value ?
      level : new goog.debug.Logger.Level(name, value);
};