# lib/goog/debug/logrecord.js

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 | * Exception text associated with the record
95 | * @type {?string}
96 | * @private
97 | */
98 | goog.debug.LogRecord.prototype.exceptionText_ = null;
99 |
100 |
101 | /**
102 | * @define {boolean} Whether to enable log sequence numbers.
103 | */
104 | goog.define('goog.debug.LogRecord.ENABLE_SEQUENCE_NUMBERS', true);
105 |
106 |
107 | /**
108 | * A sequence counter for assigning increasing sequence numbers to LogRecord
109 | * objects.
110 | * @type {number}
111 | * @private
112 | */
113 | goog.debug.LogRecord.nextSequenceNumber_ = 0;
114 |
115 |
116 | /**
117 | * Sets all fields of the log record.
118 | * @param {goog.debug.Logger.Level} level One of the level identifiers.
119 | * @param {string} msg The string message.
120 | * @param {string} loggerName The name of the source logger.
121 | * @param {number=} opt_time Time this log record was created if other than now.
122 | * If 0, we use #goog.now.
123 | * @param {number=} opt_sequenceNumber Sequence number of this log record. This
124 | * should only be passed in when restoring a log record from persistence.
125 | */
126 | goog.debug.LogRecord.prototype.reset = function(level, msg, loggerName,
127 | opt_time, opt_sequenceNumber) {
128 | if (goog.debug.LogRecord.ENABLE_SEQUENCE_NUMBERS) {
129 | this.sequenceNumber_ = typeof opt_sequenceNumber == 'number' ?
130 | opt_sequenceNumber : goog.debug.LogRecord.nextSequenceNumber_++;
131 | }
132 |
133 | this.time_ = opt_time || goog.now();
134 | this.level_ = level;
135 | this.msg_ = msg;
136 | this.loggerName_ = loggerName;
137 | delete this.exception_;
138 | delete this.exceptionText_;
139 | };
140 |
141 |
142 | /**
143 | * Get the source Logger's name.
144 | *
145 | * @return {string} source logger name (may be null).
146 | */
147 | goog.debug.LogRecord.prototype.getLoggerName = function() {
148 | return this.loggerName_;
149 | };
150 |
151 |
152 | /**
153 | * Get the exception that is part of the log record.
154 | *
155 | * @return {Object} the exception.
156 | */
157 | goog.debug.LogRecord.prototype.getException = function() {
158 | return this.exception_;
159 | };
160 |
161 |
162 | /**
163 | * Set the exception that is part of the log record.
164 | *
165 | * @param {Object} exception the exception.
166 | */
167 | goog.debug.LogRecord.prototype.setException = function(exception) {
168 | this.exception_ = exception;
169 | };
170 |
171 |
172 | /**
173 | * Get the exception text that is part of the log record.
174 | *
175 | * @return {?string} Exception text.
176 | */
177 | goog.debug.LogRecord.prototype.getExceptionText = function() {
178 | return this.exceptionText_;
179 | };
180 |
181 |
182 | /**
183 | * Set the exception text that is part of the log record.
184 | *
185 | * @param {string} text The exception text.
186 | */
187 | goog.debug.LogRecord.prototype.setExceptionText = function(text) {
188 | this.exceptionText_ = text;
189 | };
190 |
191 |
192 | /**
193 | * Get the source Logger's name.
194 | *
195 | * @param {string} loggerName source logger name (may be null).
196 | */
197 | goog.debug.LogRecord.prototype.setLoggerName = function(loggerName) {
198 | this.loggerName_ = loggerName;
199 | };
200 |
201 |
202 | /**
203 | * Get the logging message level, for example Level.SEVERE.
204 | * @return {goog.debug.Logger.Level} the logging message level.
205 | */
206 | goog.debug.LogRecord.prototype.getLevel = function() {
207 | return this.level_;
208 | };
209 |
210 |
211 | /**
212 | * Set the logging message level, for example Level.SEVERE.
213 | * @param {goog.debug.Logger.Level} level the logging message level.
214 | */
215 | goog.debug.LogRecord.prototype.setLevel = function(level) {
216 | this.level_ = level;
217 | };
218 |
219 |
220 | /**
221 | * Get the "raw" log message, before localization or formatting.
222 | *
223 | * @return {string} the raw message string.
224 | */
225 | goog.debug.LogRecord.prototype.getMessage = function() {
226 | return this.msg_;
227 | };
228 |
229 |
230 | /**
231 | * Set the "raw" log message, before localization or formatting.
232 | *
233 | * @param {string} msg the raw message string.
234 | */
235 | goog.debug.LogRecord.prototype.setMessage = function(msg) {
236 | this.msg_ = msg;
237 | };
238 |
239 |
240 | /**
241 | * Get event time in milliseconds since 1970.
242 | *
243 | * @return {number} event time in millis since 1970.
244 | */
245 | goog.debug.LogRecord.prototype.getMillis = function() {
246 | return this.time_;
247 | };
248 |
249 |
250 | /**
251 | * Set event time in milliseconds since 1970.
252 | *
253 | * @param {number} time event time in millis since 1970.
254 | */
255 | goog.debug.LogRecord.prototype.setMillis = function(time) {
256 | this.time_ = time;
257 | };
258 |
259 |
260 | /**
261 | * Get the sequence number.
262 | * <p>
263 | * Sequence numbers are normally assigned in the LogRecord
264 | * constructor, which assigns unique sequence numbers to
265 | * each new LogRecord in increasing order.
266 | * @return {number} the sequence number.
267 | */
268 | goog.debug.LogRecord.prototype.getSequenceNumber = function() {
269 | return this.sequenceNumber_;
270 | };
271 |