Path: blob/trunk/third_party/closure/goog/debug/logrecordserializer.js
2868 views
// Copyright 2011 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Static methods for serializing and deserializing log16* messages. These methods are deliberately kept separate from logrecord.js17* and logger.js because they add dependencies on goog.json and goog.object.18*19*/2021goog.provide('goog.debug.logRecordSerializer');2223goog.require('goog.debug.LogRecord');24goog.require('goog.debug.Logger');25goog.require('goog.json');26goog.require('goog.object');272829/**30* Enumeration of object keys used when serializing a log message.31* @enum {string}32* @private33*/34goog.debug.logRecordSerializer.Param_ = {35TIME: 't',36LEVEL_NAME: 'ln',37LEVEL_VALUE: 'lv',38MSG: 'm',39LOGGER_NAME: 'n',40SEQUENCE_NUMBER: 's',41EXCEPTION: 'e'42};434445/**46* Serializes a LogRecord to a JSON string. Note that any associated47* exception is likely to be lost.48* @param {goog.debug.LogRecord} record The record to serialize.49* @return {string} Serialized JSON string of the log message.50*/51goog.debug.logRecordSerializer.serialize = function(record) {52var param = goog.debug.logRecordSerializer.Param_;53return goog.json.serialize(54goog.object.create(55param.TIME, record.getMillis(), param.LEVEL_NAME,56record.getLevel().name, param.LEVEL_VALUE, record.getLevel().value,57param.MSG, record.getMessage(), param.LOGGER_NAME,58record.getLoggerName(), param.SEQUENCE_NUMBER,59record.getSequenceNumber(), param.EXCEPTION,60record.getException() && record.getException().message));61};626364/**65* Deserializes a JSON-serialized LogRecord.66* @param {string} s The JSON serialized record.67* @return {!goog.debug.LogRecord} The deserialized record.68*/69goog.debug.logRecordSerializer.parse = function(s) {70return goog.debug.logRecordSerializer.reconstitute_(71/** @type {!Object} */ (JSON.parse(s)));72};737475/**76* Reconstitutes LogRecord from the JSON object.77* @param {Object} o The JSON object.78* @return {!goog.debug.LogRecord} The reconstituted record.79* @private80*/81goog.debug.logRecordSerializer.reconstitute_ = function(o) {82var param = goog.debug.logRecordSerializer.Param_;83var level = goog.debug.logRecordSerializer.getLevel_(84o[param.LEVEL_NAME], o[param.LEVEL_VALUE]);8586var ret = new goog.debug.LogRecord(87level, o[param.MSG], o[param.LOGGER_NAME], o[param.TIME],88o[param.SEQUENCE_NUMBER]);89var exceptionMessage = o[param.EXCEPTION];90if (goog.isDefAndNotNull(exceptionMessage)) {91ret.setException(new Error(exceptionMessage));92}93return ret;94};959697/**98* @param {string} name The name of the log level to return.99* @param {number} value The numeric value of the log level to return.100* @return {!goog.debug.Logger.Level} Returns a goog.debug.Logger.Level with101* the specified name and value. If the name and value match a predefined102* log level, that instance will be returned, otherwise a new one will be103* created.104* @private105*/106goog.debug.logRecordSerializer.getLevel_ = function(name, value) {107var level = goog.debug.Logger.Level.getPredefinedLevel(name);108return level && level.value == value ? level : new goog.debug.Logger.Level(109name, value);110};111112113