logrecord.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright 2006 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 Definition of the LogRecord class. Please minimize
  16. * dependencies this file has on other closure classes as any dependency it
  17. * takes won't be able to use the logging infrastructure.
  18. *
  19. */
  20. goog.provide('goog.debug.LogRecord');
  21. /**
  22. * LogRecord objects are used to pass logging requests between
  23. * the logging framework and individual log Handlers.
  24. * @constructor
  25. * @param {goog.debug.Logger.Level} level One of the level identifiers.
  26. * @param {string} msg The string message.
  27. * @param {string} loggerName The name of the source logger.
  28. * @param {number=} opt_time Time this log record was created if other than now.
  29. * If 0, we use #goog.now.
  30. * @param {number=} opt_sequenceNumber Sequence number of this log record. This
  31. * should only be passed in when restoring a log record from persistence.
  32. */
  33. goog.debug.LogRecord = function(
  34. level, msg, loggerName, opt_time, opt_sequenceNumber) {
  35. this.reset(level, msg, loggerName, opt_time, opt_sequenceNumber);
  36. };
  37. /**
  38. * Time the LogRecord was created.
  39. * @type {number}
  40. * @private
  41. */
  42. goog.debug.LogRecord.prototype.time_;
  43. /**
  44. * Level of the LogRecord
  45. * @type {goog.debug.Logger.Level}
  46. * @private
  47. */
  48. goog.debug.LogRecord.prototype.level_;
  49. /**
  50. * Message associated with the record
  51. * @type {string}
  52. * @private
  53. */
  54. goog.debug.LogRecord.prototype.msg_;
  55. /**
  56. * Name of the logger that created the record.
  57. * @type {string}
  58. * @private
  59. */
  60. goog.debug.LogRecord.prototype.loggerName_;
  61. /**
  62. * Sequence number for the LogRecord. Each record has a unique sequence number
  63. * that is greater than all log records created before it.
  64. * @type {number}
  65. * @private
  66. */
  67. goog.debug.LogRecord.prototype.sequenceNumber_ = 0;
  68. /**
  69. * Exception associated with the record
  70. * @type {Object}
  71. * @private
  72. */
  73. goog.debug.LogRecord.prototype.exception_ = null;
  74. /**
  75. * @define {boolean} Whether to enable log sequence numbers.
  76. */
  77. goog.define('goog.debug.LogRecord.ENABLE_SEQUENCE_NUMBERS', true);
  78. /**
  79. * A sequence counter for assigning increasing sequence numbers to LogRecord
  80. * objects.
  81. * @type {number}
  82. * @private
  83. */
  84. goog.debug.LogRecord.nextSequenceNumber_ = 0;
  85. /**
  86. * Sets all fields of the log record.
  87. * @param {goog.debug.Logger.Level} level One of the level identifiers.
  88. * @param {string} msg The string message.
  89. * @param {string} loggerName The name of the source logger.
  90. * @param {number=} opt_time Time this log record was created if other than now.
  91. * If 0, we use #goog.now.
  92. * @param {number=} opt_sequenceNumber Sequence number of this log record. This
  93. * should only be passed in when restoring a log record from persistence.
  94. */
  95. goog.debug.LogRecord.prototype.reset = function(
  96. level, msg, loggerName, opt_time, opt_sequenceNumber) {
  97. if (goog.debug.LogRecord.ENABLE_SEQUENCE_NUMBERS) {
  98. this.sequenceNumber_ = typeof opt_sequenceNumber == 'number' ?
  99. opt_sequenceNumber :
  100. goog.debug.LogRecord.nextSequenceNumber_++;
  101. }
  102. this.time_ = opt_time || goog.now();
  103. this.level_ = level;
  104. this.msg_ = msg;
  105. this.loggerName_ = loggerName;
  106. delete this.exception_;
  107. };
  108. /**
  109. * Get the source Logger's name.
  110. *
  111. * @return {string} source logger name (may be null).
  112. */
  113. goog.debug.LogRecord.prototype.getLoggerName = function() {
  114. return this.loggerName_;
  115. };
  116. /**
  117. * Get the exception that is part of the log record.
  118. *
  119. * @return {Object} the exception.
  120. */
  121. goog.debug.LogRecord.prototype.getException = function() {
  122. return this.exception_;
  123. };
  124. /**
  125. * Set the exception that is part of the log record.
  126. *
  127. * @param {Object} exception the exception.
  128. */
  129. goog.debug.LogRecord.prototype.setException = function(exception) {
  130. this.exception_ = exception;
  131. };
  132. /**
  133. * Get the source Logger's name.
  134. *
  135. * @param {string} loggerName source logger name (may be null).
  136. */
  137. goog.debug.LogRecord.prototype.setLoggerName = function(loggerName) {
  138. this.loggerName_ = loggerName;
  139. };
  140. /**
  141. * Get the logging message level, for example Level.SEVERE.
  142. * @return {goog.debug.Logger.Level} the logging message level.
  143. */
  144. goog.debug.LogRecord.prototype.getLevel = function() {
  145. return this.level_;
  146. };
  147. /**
  148. * Set the logging message level, for example Level.SEVERE.
  149. * @param {goog.debug.Logger.Level} level the logging message level.
  150. */
  151. goog.debug.LogRecord.prototype.setLevel = function(level) {
  152. this.level_ = level;
  153. };
  154. /**
  155. * Get the "raw" log message, before localization or formatting.
  156. *
  157. * @return {string} the raw message string.
  158. */
  159. goog.debug.LogRecord.prototype.getMessage = function() {
  160. return this.msg_;
  161. };
  162. /**
  163. * Set the "raw" log message, before localization or formatting.
  164. *
  165. * @param {string} msg the raw message string.
  166. */
  167. goog.debug.LogRecord.prototype.setMessage = function(msg) {
  168. this.msg_ = msg;
  169. };
  170. /**
  171. * Get event time in milliseconds since 1970.
  172. *
  173. * @return {number} event time in millis since 1970.
  174. */
  175. goog.debug.LogRecord.prototype.getMillis = function() {
  176. return this.time_;
  177. };
  178. /**
  179. * Set event time in milliseconds since 1970.
  180. *
  181. * @param {number} time event time in millis since 1970.
  182. */
  183. goog.debug.LogRecord.prototype.setMillis = function(time) {
  184. this.time_ = time;
  185. };
  186. /**
  187. * Get the sequence number.
  188. * <p>
  189. * Sequence numbers are normally assigned in the LogRecord
  190. * constructor, which assigns unique sequence numbers to
  191. * each new LogRecord in increasing order.
  192. * @return {number} the sequence number.
  193. */
  194. goog.debug.LogRecord.prototype.getSequenceNumber = function() {
  195. return this.sequenceNumber_;
  196. };