logrecordserializer.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * @fileoverview Static methods for serializing and deserializing log
  16. * messages. These methods are deliberately kept separate from logrecord.js
  17. * and logger.js because they add dependencies on goog.json and goog.object.
  18. *
  19. */
  20. goog.provide('goog.debug.logRecordSerializer');
  21. goog.require('goog.debug.LogRecord');
  22. goog.require('goog.debug.Logger');
  23. goog.require('goog.json');
  24. goog.require('goog.object');
  25. /**
  26. * Enumeration of object keys used when serializing a log message.
  27. * @enum {string}
  28. * @private
  29. */
  30. goog.debug.logRecordSerializer.Param_ = {
  31. TIME: 't',
  32. LEVEL_NAME: 'ln',
  33. LEVEL_VALUE: 'lv',
  34. MSG: 'm',
  35. LOGGER_NAME: 'n',
  36. SEQUENCE_NUMBER: 's',
  37. EXCEPTION: 'e'
  38. };
  39. /**
  40. * Serializes a LogRecord to a JSON string. Note that any associated
  41. * exception is likely to be lost.
  42. * @param {goog.debug.LogRecord} record The record to serialize.
  43. * @return {string} Serialized JSON string of the log message.
  44. */
  45. goog.debug.logRecordSerializer.serialize = function(record) {
  46. var param = goog.debug.logRecordSerializer.Param_;
  47. return goog.json.serialize(
  48. goog.object.create(
  49. param.TIME, record.getMillis(), param.LEVEL_NAME,
  50. record.getLevel().name, param.LEVEL_VALUE, record.getLevel().value,
  51. param.MSG, record.getMessage(), param.LOGGER_NAME,
  52. record.getLoggerName(), param.SEQUENCE_NUMBER,
  53. record.getSequenceNumber(), param.EXCEPTION,
  54. record.getException() && record.getException().message));
  55. };
  56. /**
  57. * Deserializes a JSON-serialized LogRecord.
  58. * @param {string} s The JSON serialized record.
  59. * @return {!goog.debug.LogRecord} The deserialized record.
  60. */
  61. goog.debug.logRecordSerializer.parse = function(s) {
  62. return goog.debug.logRecordSerializer.reconstitute_(
  63. /** @type {!Object} */ (JSON.parse(s)));
  64. };
  65. /**
  66. * Reconstitutes LogRecord from the JSON object.
  67. * @param {Object} o The JSON object.
  68. * @return {!goog.debug.LogRecord} The reconstituted record.
  69. * @private
  70. */
  71. goog.debug.logRecordSerializer.reconstitute_ = function(o) {
  72. var param = goog.debug.logRecordSerializer.Param_;
  73. var level = goog.debug.logRecordSerializer.getLevel_(
  74. o[param.LEVEL_NAME], o[param.LEVEL_VALUE]);
  75. var ret = new goog.debug.LogRecord(
  76. level, o[param.MSG], o[param.LOGGER_NAME], o[param.TIME],
  77. o[param.SEQUENCE_NUMBER]);
  78. var exceptionMessage = o[param.EXCEPTION];
  79. if (goog.isDefAndNotNull(exceptionMessage)) {
  80. ret.setException(new Error(exceptionMessage));
  81. }
  82. return ret;
  83. };
  84. /**
  85. * @param {string} name The name of the log level to return.
  86. * @param {number} value The numeric value of the log level to return.
  87. * @return {!goog.debug.Logger.Level} Returns a goog.debug.Logger.Level with
  88. * the specified name and value. If the name and value match a predefined
  89. * log level, that instance will be returned, otherwise a new one will be
  90. * created.
  91. * @private
  92. */
  93. goog.debug.logRecordSerializer.getLevel_ = function(name, value) {
  94. var level = goog.debug.Logger.Level.getPredefinedLevel(name);
  95. return level && level.value == value ? level : new goog.debug.Logger.Level(
  96. name, value);
  97. };