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