Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/log/log.js
4532 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Basic strippable logging definitions.
9
* @see http://go/closurelogging
10
*/
11
12
goog.provide('goog.log');
13
goog.provide('goog.log.Level');
14
goog.provide('goog.log.LogBuffer');
15
goog.provide('goog.log.LogRecord');
16
goog.provide('goog.log.Logger');
17
18
goog.require('goog.asserts');
19
goog.require('goog.debug');
20
21
22
/**
23
* A message value that can be handled by a goog.log.Logger.
24
*
25
* Functions are treated like callbacks, but are only called when the event's
26
* log level is enabled. This is useful for logging messages that are expensive
27
* to construct.
28
*
29
* @typedef {string|function(): string}
30
*/
31
goog.log.Loggable;
32
33
/** @define {boolean} Whether logging is enabled. */
34
goog.log.ENABLED = goog.define('goog.log.ENABLED', goog.debug.LOGGING_ENABLED);
35
36
/** @const */
37
goog.log.ROOT_LOGGER_NAME = '';
38
39
40
// TODO(user): Make goog.log.Level an enum.
41
/**
42
* The goog.log.Level class defines a set of standard logging levels that
43
* can be used to control logging output. The logging goog.log.Level objects
44
* are ordered and are specified by ordered integers. Enabling logging
45
* at a given level also enables logging at all higher levels.
46
* <p>
47
* Clients should normally use the predefined goog.log.Level constants such
48
* as goog.log.Level.SEVERE.
49
* <p>
50
* The levels in descending order are:
51
* <ul>
52
* <li>SEVERE (highest value)
53
* <li>WARNING
54
* <li>INFO
55
* <li>CONFIG
56
* <li>FINE
57
* <li>FINER
58
* <li>FINEST (lowest value)
59
* </ul>
60
* In addition there is a level OFF that can be used to turn
61
* off logging, and a level ALL that can be used to enable
62
* logging of all messages.
63
*
64
* @final
65
*/
66
goog.log.Level = class Level {
67
/**
68
* @param {string} name The name of the level.
69
* @param {number} value The numeric value of the level.
70
*/
71
constructor(name, value) {
72
/**
73
* The name of the level
74
* @type {string}
75
* @const
76
*/
77
this.name = name;
78
79
/**
80
* The numeric value of the level
81
* @type {number}
82
*/
83
this.value = value;
84
}
85
86
/**
87
* @return {string} String representation of the logger level.
88
* @override
89
*/
90
toString() {
91
return this.name;
92
}
93
};
94
95
96
/**
97
* OFF is a special level that can be used to turn off logging.
98
* This level is initialized to <CODE>Infinity</CODE>.
99
* @type {!goog.log.Level}
100
*/
101
goog.log.Level.OFF = new goog.log.Level('OFF', Infinity);
102
103
104
/**
105
* SHOUT is a message level for extra debugging loudness.
106
* This level is initialized to <CODE>1200</CODE>.
107
* @type {!goog.log.Level}
108
*/
109
goog.log.Level.SHOUT = new goog.log.Level('SHOUT', 1200);
110
111
112
/**
113
* SEVERE is a message level indicating a serious failure.
114
* This level is initialized to <CODE>1000</CODE>.
115
* @type {!goog.log.Level}
116
*/
117
goog.log.Level.SEVERE = new goog.log.Level('SEVERE', 1000);
118
119
120
/**
121
* WARNING is a message level indicating a potential problem.
122
* This level is initialized to <CODE>900</CODE>.
123
* @type {!goog.log.Level}
124
*/
125
goog.log.Level.WARNING = new goog.log.Level('WARNING', 900);
126
127
128
/**
129
* INFO is a message level for informational messages.
130
* This level is initialized to <CODE>800</CODE>.
131
* @type {!goog.log.Level}
132
*/
133
goog.log.Level.INFO = new goog.log.Level('INFO', 800);
134
135
136
/**
137
* CONFIG is a message level for static configuration messages.
138
* This level is initialized to <CODE>700</CODE>.
139
* @type {!goog.log.Level}
140
*/
141
goog.log.Level.CONFIG = new goog.log.Level('CONFIG', 700);
142
143
144
/**
145
* FINE is a message level providing tracing information.
146
* This level is initialized to <CODE>500</CODE>.
147
* @type {!goog.log.Level}
148
*/
149
goog.log.Level.FINE = new goog.log.Level('FINE', 500);
150
151
152
/**
153
* FINER indicates a fairly detailed tracing message.
154
* This level is initialized to <CODE>400</CODE>.
155
* @type {!goog.log.Level}
156
*/
157
goog.log.Level.FINER = new goog.log.Level('FINER', 400);
158
159
/**
160
* FINEST indicates a highly detailed tracing message.
161
* This level is initialized to <CODE>300</CODE>.
162
* @type {!goog.log.Level}
163
*/
164
165
goog.log.Level.FINEST = new goog.log.Level('FINEST', 300);
166
167
168
/**
169
* ALL indicates that all messages should be logged.
170
* This level is initialized to <CODE>0</CODE>.
171
* @type {!goog.log.Level}
172
*/
173
goog.log.Level.ALL = new goog.log.Level('ALL', 0);
174
175
176
/**
177
* The predefined levels.
178
* @type {!Array<!goog.log.Level>}
179
* @final
180
*/
181
goog.log.Level.PREDEFINED_LEVELS = [
182
goog.log.Level.OFF, goog.log.Level.SHOUT, goog.log.Level.SEVERE,
183
goog.log.Level.WARNING, goog.log.Level.INFO, goog.log.Level.CONFIG,
184
goog.log.Level.FINE, goog.log.Level.FINER, goog.log.Level.FINEST,
185
goog.log.Level.ALL
186
];
187
188
189
/**
190
* A lookup map used to find the level object based on the name or value of
191
* the level object.
192
* @type {?Object}
193
* @private
194
*/
195
goog.log.Level.predefinedLevelsCache_ = null;
196
197
198
/**
199
* Creates the predefined levels cache and populates it.
200
* @private
201
*/
202
goog.log.Level.createPredefinedLevelsCache_ = function() {
203
goog.log.Level.predefinedLevelsCache_ = {};
204
for (let i = 0, level; level = goog.log.Level.PREDEFINED_LEVELS[i]; i++) {
205
goog.log.Level.predefinedLevelsCache_[level.value] = level;
206
goog.log.Level.predefinedLevelsCache_[level.name] = level;
207
}
208
};
209
210
211
/**
212
* Gets the predefined level with the given name.
213
* @param {string} name The name of the level.
214
* @return {!goog.log.Level|null} The level, or null if none found.
215
*/
216
goog.log.Level.getPredefinedLevel = function(name) {
217
if (!goog.log.Level.predefinedLevelsCache_) {
218
goog.log.Level.createPredefinedLevelsCache_();
219
}
220
221
return goog.log.Level.predefinedLevelsCache_[name] || null;
222
};
223
224
225
/**
226
* Gets the highest predefined level <= #value.
227
* @param {number} value goog.log.Level value.
228
* @return {!goog.log.Level|null} The level, or null if none found.
229
*/
230
goog.log.Level.getPredefinedLevelByValue = function(value) {
231
if (!goog.log.Level.predefinedLevelsCache_) {
232
goog.log.Level.createPredefinedLevelsCache_();
233
}
234
235
if (value in /** @type {!Object} */ (goog.log.Level.predefinedLevelsCache_)) {
236
return goog.log.Level.predefinedLevelsCache_[value];
237
}
238
239
for (let i = 0; i < goog.log.Level.PREDEFINED_LEVELS.length; ++i) {
240
let level = goog.log.Level.PREDEFINED_LEVELS[i];
241
if (level.value <= value) {
242
return level;
243
}
244
}
245
return null;
246
};
247
248
249
/** @interface */
250
goog.log.Logger = class Logger {
251
/**
252
* Gets the name of the Logger.
253
* @return {string}
254
* @public
255
*/
256
getName() {}
257
};
258
259
260
/**
261
* Only for compatibility with goog.debug.Logger.Level, which is how many users
262
* access Level.
263
* TODO(user): Remove these definitions.
264
* @final
265
*/
266
goog.log.Logger.Level = goog.log.Level;
267
268
269
/**
270
* A buffer for log records. The purpose of this is to improve
271
* logging performance by re-using old objects when the buffer becomes full and
272
* to eliminate the need for each app to implement their own log buffer. The
273
* disadvantage to doing this is that log handlers cannot maintain references to
274
* log records and expect that they are not overwriten at a later point.
275
* @final
276
*/
277
goog.log.LogBuffer = class LogBuffer {
278
/**
279
* @param {number=} capacity The capacity of this LogBuffer instance.
280
*/
281
constructor(capacity) {
282
/**
283
* The buffer's capacity.
284
* @type {number}
285
* @private
286
*/
287
this.capacity_ =
288
typeof capacity === 'number' ? capacity : goog.log.LogBuffer.CAPACITY;
289
290
/**
291
* The array to store the records.
292
* @type {!Array<!goog.log.LogRecord|undefined>}
293
* @private
294
*/
295
this.buffer_;
296
297
/**
298
* The index of the most recently added record, or -1 if there are no
299
* records.
300
* @type {number}
301
* @private
302
*/
303
this.curIndex_;
304
305
/**
306
* Whether the buffer is at capacity.
307
* @type {boolean}
308
* @private
309
*/
310
this.isFull_;
311
312
this.clear();
313
}
314
315
316
/**
317
* Adds a log record to the buffer, possibly overwriting the oldest record.
318
* @param {!goog.log.Level} level One of the level identifiers.
319
* @param {string} msg The string message.
320
* @param {string} loggerName The name of the source logger.
321
* @return {!goog.log.LogRecord} The log record.
322
*/
323
addRecord(level, msg, loggerName) {
324
if (!this.isBufferingEnabled()) {
325
return new goog.log.LogRecord(level, msg, loggerName);
326
}
327
const curIndex = (this.curIndex_ + 1) % this.capacity_;
328
this.curIndex_ = curIndex;
329
if (this.isFull_) {
330
const ret = this.buffer_[curIndex];
331
ret.reset(level, msg, loggerName);
332
return ret;
333
}
334
this.isFull_ = curIndex == this.capacity_ - 1;
335
return this.buffer_[curIndex] =
336
new goog.log.LogRecord(level, msg, loggerName);
337
}
338
339
/**
340
* Calls the given function for each buffered log record, starting with the
341
* oldest one.
342
* TODO(user): Make this a [Symbol.iterator] once all usages of
343
* goog.debug.LogBuffer can be deleted.
344
* @param {!goog.log.LogRecordHandler} func The function to call.
345
*/
346
forEachRecord(func) {
347
const buffer = this.buffer_;
348
// Corner case: no records.
349
if (!buffer[0]) {
350
return;
351
}
352
const curIndex = this.curIndex_;
353
let i = this.isFull_ ? curIndex : -1;
354
do {
355
i = (i + 1) % this.capacity_;
356
func(/** @type {!goog.log.LogRecord} */ (buffer[i]));
357
} while (i !== curIndex);
358
}
359
360
/**
361
* @return {boolean} Whether the log buffer is enabled.
362
*/
363
isBufferingEnabled() {
364
return this.capacity_ > 0;
365
}
366
367
/**
368
* @return {boolean} Return whether the log buffer is full.
369
*/
370
isFull() {
371
return this.isFull_;
372
}
373
374
/**
375
* Removes all buffered log records.
376
*/
377
clear() {
378
this.buffer_ = new Array(this.capacity_);
379
this.curIndex_ = -1;
380
this.isFull_ = false;
381
}
382
};
383
384
385
/**
386
* @type {!goog.log.LogBuffer|undefined}
387
* @private
388
*/
389
goog.log.LogBuffer.instance_;
390
391
392
/**
393
* @define {number} The number of log records to buffer. 0 means disable
394
* buffering.
395
*/
396
goog.log.LogBuffer.CAPACITY = goog.define('goog.debug.LogBuffer.CAPACITY', 0);
397
398
399
/**
400
* A static method that always returns the same instance of goog.log.LogBuffer.
401
* @return {!goog.log.LogBuffer} The goog.log.LogBuffer singleton instance.
402
*/
403
goog.log.LogBuffer.getInstance = function() {
404
if (!goog.log.LogBuffer.instance_) {
405
goog.log.LogBuffer.instance_ =
406
new goog.log.LogBuffer(goog.log.LogBuffer.CAPACITY);
407
}
408
return goog.log.LogBuffer.instance_;
409
};
410
411
412
/**
413
* Whether the log buffer is enabled.
414
* @return {boolean}
415
*/
416
goog.log.LogBuffer.isBufferingEnabled = function() {
417
return goog.log.LogBuffer.getInstance().isBufferingEnabled();
418
};
419
420
421
/**
422
* LogRecord objects are used to pass logging requests between the logging
423
* framework and individual log handlers. These objects should not be
424
* constructed or reset by application code.
425
*/
426
goog.log.LogRecord = class LogRecord {
427
/**
428
* @param {?goog.log.Level} level One of the level identifiers.
429
* @param {string} msg The string message.
430
* @param {string} loggerName The name of the source logger.
431
* @param {number=} time Time this log record was created if other than
432
* now. If 0, we use #goog.now.
433
* @param {number=} sequenceNumber Sequence number of this log record.
434
* This should only be passed in when restoring a log record from
435
* persistence.
436
*/
437
constructor(level, msg, loggerName, time, sequenceNumber) {
438
/**
439
* Level of the LogRecord.
440
* @type {!goog.log.Level}
441
* @private
442
*/
443
this.level_;
444
445
/**
446
* Name of the logger that created the record.
447
* @type {string}
448
* @private
449
*/
450
this.loggerName_;
451
452
/**
453
* Message associated with the record
454
* @type {string}
455
* @private
456
*/
457
this.msg_;
458
459
/**
460
* Time the LogRecord was created.
461
* @type {number}
462
* @private
463
*/
464
this.time_;
465
466
/**
467
* Sequence number for the LogRecord. Each record has a unique sequence
468
* number that is greater than all log records created before it.
469
* @type {number}
470
* @private
471
*/
472
this.sequenceNumber_;
473
474
/**
475
* Exception associated with the record
476
* @type {*}
477
* @private
478
*/
479
this.exception_ = undefined;
480
481
this.reset(
482
level || goog.log.Level.OFF, msg, loggerName, time, sequenceNumber);
483
};
484
485
/**
486
* Sets all fields of the log record.
487
* @param {!goog.log.Level} level One of the level identifiers.
488
* @param {string} msg The string message.
489
* @param {string} loggerName The name of the source logger.
490
* @param {number=} time Time this log record was created if other than
491
* now. If 0, we use #goog.now.
492
* @param {number=} sequenceNumber Sequence number of this log record.
493
* This should only be passed in when restoring a log record from
494
* persistence.
495
*/
496
reset(level, msg, loggerName, time, sequenceNumber) {
497
this.time_ = time || goog.now();
498
this.level_ = level;
499
this.msg_ = msg;
500
this.loggerName_ = loggerName;
501
this.exception_ = undefined;
502
this.sequenceNumber_ = typeof sequenceNumber === 'number' ?
503
sequenceNumber :
504
goog.log.LogRecord.nextSequenceNumber_;
505
};
506
507
508
/**
509
* Gets the source Logger's name.
510
*
511
* @return {string} source logger name (may be null).
512
*/
513
getLoggerName() {
514
return this.loggerName_;
515
};
516
517
518
/**
519
* Sets the source Logger's name.
520
*
521
* @param {string} name The logger name.
522
*/
523
setLoggerName(name) {
524
this.loggerName_ = name;
525
};
526
527
528
/**
529
* Gets the exception that is part of the log record.
530
*
531
* @return {*} the exception.
532
*/
533
getException() {
534
return this.exception_;
535
};
536
537
538
/**
539
* Sets the exception that is part of the log record.
540
* @param {*} exception the exception.
541
*/
542
setException(exception) {
543
this.exception_ = exception;
544
};
545
546
547
/**
548
* Gets the logging message level, for example Level.SEVERE.
549
* @return {!goog.log.Level} the logging message level.
550
*/
551
getLevel() {
552
return this.level_;
553
};
554
555
556
/**
557
* Sets the logging message level, for example Level.SEVERE.
558
* @param {!goog.log.Level} level the logging message level.
559
*/
560
setLevel(level) {
561
this.level_ = level;
562
};
563
564
565
/**
566
* Gets the "raw" log message, before localization or formatting.
567
* @return {string} the raw message string.
568
*/
569
getMessage() {
570
return this.msg_;
571
};
572
573
574
/**
575
* Sets the "raw" log message, before localization or formatting.
576
*
577
* @param {string} msg the raw message string.
578
*/
579
setMessage(msg) {
580
this.msg_ = msg;
581
};
582
583
584
/**
585
* Gets event time in milliseconds since 1970.
586
* @return {number} event time in millis since 1970.
587
*/
588
getMillis() {
589
return this.time_;
590
};
591
592
593
/**
594
* Sets event time in milliseconds since 1970.
595
* @param {number} time event time in millis since 1970.
596
*/
597
setMillis(time) {
598
this.time_ = time;
599
};
600
601
602
/**
603
* Gets the sequence number. Sequence numbers are normally assigned when a
604
* LogRecord is constructed or reset in incrementally increasing order.
605
* @return {number}
606
*/
607
getSequenceNumber() {
608
return this.sequenceNumber_;
609
};
610
};
611
612
613
/**
614
* A sequence counter for assigning increasing sequence numbers to LogRecord
615
* objects.
616
* @type {number}
617
* @private
618
*/
619
goog.log.LogRecord.nextSequenceNumber_ = 0;
620
621
622
/**
623
* A type that describes a function that handles logs.
624
* @typedef {function(!goog.log.LogRecord): ?}
625
*/
626
goog.log.LogRecordHandler;
627
628
629
/**
630
* A LogRegistryEntry_ contains data about a Logger.
631
* @final
632
*/
633
goog.log.LogRegistryEntry_ = class LogRegistryEntry_ {
634
/**
635
* @param {string} name
636
* @param {!goog.log.LogRegistryEntry_|null=} parent
637
*/
638
constructor(name, parent = null) {
639
/**
640
* The minimum log level that a message must be for it to be logged by the
641
* Logger corresponding to this LogRegistryEntry_. If null, the parent's
642
* log level is used instead.
643
* @type {?goog.log.Level}
644
*/
645
this.level = null;
646
647
/**
648
* A list of functions that will be called when the Logger corresponding to
649
* this LogRegistryEntry_ is used to log a message.
650
* @type {!Array<!goog.log.LogRecordHandler>}
651
*/
652
this.handlers = [];
653
654
/**
655
* A reference to LogRegistryEntry_ objects that correspond to the direct
656
* ancestor of the Logger represented by this LogRegistryEntry_ object
657
* (via name, treated as a dot-separated namespace).
658
* @type {!goog.log.LogRegistryEntry_|null}
659
*/
660
this.parent = parent || null;
661
662
/**
663
* A list of references to LogRegistryEntry_ objects that correspond to the
664
* direct descendants of the Logger represented by this LogRegistryEntry_
665
* object (via name, treated as a dot-separated namespace).
666
* @type {!Array<!goog.log.LogRegistryEntry_>}
667
*/
668
this.children = [];
669
670
/**
671
* A reference to the Logger itself.
672
* @type {!goog.log.Logger}
673
*/
674
this.logger = /** @type {!goog.log.Logger} */ ({getName: () => name});
675
}
676
677
/**
678
* Returns the effective level of the logger based on its ancestors' levels.
679
* @return {!goog.log.Level} The level.
680
*/
681
getEffectiveLevel() {
682
if (this.level) {
683
return this.level;
684
} else if (this.parent) {
685
return this.parent.getEffectiveLevel();
686
}
687
goog.asserts.fail('Root logger has no level set.');
688
return goog.log.Level.OFF;
689
};
690
691
/**
692
* Calls the log handlers associated with this Logger, followed by those of
693
* its parents, etc. until the root Logger's associated log handlers are
694
* called.
695
* @param {!goog.log.LogRecord} logRecord The log record to pass to each
696
* handler.
697
*/
698
publish(logRecord) {
699
let target = this;
700
while (target) {
701
target.handlers.forEach(handler => {
702
handler(logRecord);
703
});
704
target = target.parent;
705
}
706
}
707
};
708
709
710
/**
711
* A LogRegistry_ owns references to all loggers, and is responsible for storing
712
* all the internal state needed for loggers to operate correctly.
713
*
714
* @final
715
*/
716
goog.log.LogRegistry_ = class LogRegistry_ {
717
constructor() {
718
/**
719
* Per-log information retained by this LogRegistry_.
720
* @type {!Object<string, !goog.log.LogRegistryEntry_>}
721
*/
722
this.entries = {};
723
724
// The root logger.
725
const rootLogRegistryEntry =
726
new goog.log.LogRegistryEntry_(goog.log.ROOT_LOGGER_NAME);
727
rootLogRegistryEntry.level = goog.log.Level.CONFIG;
728
this.entries[goog.log.ROOT_LOGGER_NAME] = rootLogRegistryEntry;
729
}
730
731
/**
732
* Gets the LogRegistry_ entry under the given name, creating the entry if one
733
* doesn't already exist.
734
* @param {string} name The name to look up.
735
* @param {?goog.log.Level=} level If provided, override the default logging
736
* level of the returned Logger with the provided level.
737
* @return {!goog.log.LogRegistryEntry_}
738
*/
739
getLogRegistryEntry(name, level) {
740
const entry = this.entries[name];
741
if (entry) {
742
if (level !== undefined) {
743
entry.level = level;
744
}
745
return entry;
746
} else {
747
// The logger and its associated registry entry needs to be created.
748
749
// Get its parent first.
750
const lastDotIndex = name.lastIndexOf('.');
751
const parentName = name.slice(0, Math.max(lastDotIndex, 0));
752
const parentLogRegistryEntry = this.getLogRegistryEntry(parentName);
753
754
// Now create the new entry, linking it with its parent.
755
const logRegistryEntry =
756
new goog.log.LogRegistryEntry_(name, parentLogRegistryEntry);
757
this.entries[name] = logRegistryEntry;
758
parentLogRegistryEntry.children.push(logRegistryEntry);
759
760
if (level !== undefined) {
761
logRegistryEntry.level = level;
762
}
763
764
return logRegistryEntry;
765
}
766
}
767
768
/**
769
* Get a list of all loggers.
770
* @return {!Array<!goog.log.Logger>}
771
*/
772
getAllLoggers() {
773
return Object.keys(this.entries)
774
.map(loggerName => this.entries[loggerName].logger);
775
}
776
};
777
778
/**
779
* A static method that always returns the same instance of LogRegistry_.
780
* @return {!goog.log.LogRegistry_} The LogRegistry_ singleton instance.
781
*/
782
goog.log.LogRegistry_.getInstance = function() {
783
if (!goog.log.LogRegistry_.instance_) {
784
goog.log.LogRegistry_.instance_ = new goog.log.LogRegistry_();
785
}
786
return /** @type {!goog.log.LogRegistry_} */ (
787
goog.log.LogRegistry_.instance_);
788
};
789
790
/**
791
* @type {!goog.log.LogRegistry_|undefined}
792
* @private
793
*/
794
goog.log.LogRegistry_.instance_;
795
796
797
/**
798
* Finds or creates a logger for a named subsystem. If a logger has already been
799
* created with the given name it is returned. Otherwise, a new logger is
800
* created. If a new logger is created, it will be configured to send logging
801
* output to its parent's handlers.
802
*
803
* @param {string} name A name for the logger. This should be a dot-separated
804
* name and should normally be based on the package name or class name of
805
* the subsystem, such as goog.net.BrowserChannel.
806
* @param {?goog.log.Level=} level If provided, override the default logging
807
* level with the provided level. This parameter is deprecated; prefer using
808
* goog.log.setLevel to set the logger's level instead.
809
* TODO(user): Delete this parameter.
810
* @return {!goog.log.Logger|null} The named logger, or null if logging is
811
* disabled.
812
*/
813
goog.log.getLogger = function(name, level) {
814
if (goog.log.ENABLED) {
815
const loggerEntry =
816
goog.log.LogRegistry_.getInstance().getLogRegistryEntry(name, level);
817
return loggerEntry.logger;
818
} else {
819
return null;
820
}
821
};
822
823
824
/**
825
* Returns the root logger.
826
*
827
* @return {!goog.log.Logger|null} The root logger, or null if logging is
828
* disabled.
829
*/
830
goog.log.getRootLogger = function() {
831
if (goog.log.ENABLED) {
832
const loggerEntry = goog.log.LogRegistry_.getInstance().getLogRegistryEntry(
833
goog.log.ROOT_LOGGER_NAME);
834
return loggerEntry.logger;
835
} else {
836
return null;
837
}
838
};
839
840
841
// TODO(johnlenz): try to tighten the types to these functions.
842
/**
843
* Adds a handler to the logger. This doesn't use the event system because
844
* we want to be able to add logging to the event system.
845
* @param {?goog.log.Logger} logger
846
* @param {!goog.log.LogRecordHandler} handler Handler function to
847
* add.
848
*/
849
goog.log.addHandler = function(logger, handler) {
850
if (goog.log.ENABLED && logger) {
851
const loggerEntry = goog.log.LogRegistry_.getInstance().getLogRegistryEntry(
852
logger.getName());
853
loggerEntry.handlers.push(handler);
854
}
855
};
856
857
858
/**
859
* Removes a handler from the logger. This doesn't use the event system because
860
* we want to be able to add logging to the event system.
861
* @param {?goog.log.Logger} logger
862
* @param {!goog.log.LogRecordHandler} handler Handler function to
863
* remove.
864
* @return {boolean} Whether the handler was removed.
865
*/
866
goog.log.removeHandler = function(logger, handler) {
867
if (goog.log.ENABLED && logger) {
868
const loggerEntry = goog.log.LogRegistry_.getInstance().getLogRegistryEntry(
869
logger.getName());
870
const indexOfHandler = loggerEntry.handlers.indexOf(handler);
871
if (indexOfHandler !== -1) {
872
loggerEntry.handlers.splice(indexOfHandler, 1);
873
return true;
874
}
875
}
876
return false;
877
};
878
879
880
/**
881
* Set the log level specifying which message levels will be logged by this
882
* logger. Message levels lower than this value will be discarded.
883
* The level value goog.log.Level.OFF can be used to turn off logging. If the
884
* new level is null, it means that this node should inherit its level from its
885
* nearest ancestor with a specific (non-null) level value.
886
*
887
* @param {?goog.log.Logger} logger
888
* @param {!goog.log.Level|null} level The new level.
889
*/
890
goog.log.setLevel = function(logger, level) {
891
if (goog.log.ENABLED && logger) {
892
const loggerEntry = goog.log.LogRegistry_.getInstance().getLogRegistryEntry(
893
logger.getName());
894
loggerEntry.level = level;
895
}
896
};
897
898
899
/**
900
* Gets the log level specifying which message levels will be logged by this
901
* logger. Message levels lower than this value will be discarded.
902
* The level value goog.log.Level.OFF can be used to turn off logging. If the
903
* level is null, it means that this node should inherit its level from its
904
* nearest ancestor with a specific (non-null) level value.
905
*
906
* @param {?goog.log.Logger} logger
907
* @return {!goog.log.Level|null} The level.
908
*/
909
goog.log.getLevel = function(logger) {
910
if (goog.log.ENABLED && logger) {
911
const loggerEntry = goog.log.LogRegistry_.getInstance().getLogRegistryEntry(
912
logger.getName());
913
return loggerEntry.level;
914
}
915
return null;
916
};
917
918
919
/**
920
* Returns the effective level of the logger based on its ancestors' levels.
921
* @param {?goog.log.Logger} logger
922
* @return {!goog.log.Level} The level.
923
*/
924
goog.log.getEffectiveLevel = function(logger) {
925
if (goog.log.ENABLED && logger) {
926
const loggerEntry = goog.log.LogRegistry_.getInstance().getLogRegistryEntry(
927
logger.getName());
928
return loggerEntry.getEffectiveLevel();
929
}
930
return goog.log.Level.OFF;
931
};
932
933
934
/**
935
* Checks if a message of the given level would actually be logged by this
936
* logger. This check is based on the goog.log.Loggers effective level, which
937
* may be inherited from its parent.
938
* @param {?goog.log.Logger} logger
939
* @param {?goog.log.Level} level The level to check.
940
* @return {boolean} Whether the message would be logged.
941
*/
942
goog.log.isLoggable = function(logger, level) {
943
if (goog.log.ENABLED && logger && level) {
944
return level.value >= goog.log.getEffectiveLevel(logger).value;
945
}
946
return false;
947
};
948
949
950
/**
951
* Gets a list of all loggers.
952
* @return {!Array<!goog.log.Logger>}
953
*/
954
goog.log.getAllLoggers = function() {
955
if (goog.log.ENABLED) {
956
return goog.log.LogRegistry_.getInstance().getAllLoggers();
957
}
958
return [];
959
};
960
961
962
/**
963
* Creates a log record. If the logger is currently enabled for the
964
* given message level then the given message is forwarded to all the
965
* registered output Handler objects.
966
* TODO(user): Delete this method from the public API.
967
* @param {?goog.log.Logger} logger
968
* @param {?goog.log.Level} level One of the level identifiers.
969
* @param {string} msg The message to log.
970
* @param {*=} exception An exception associated with the message.
971
* @return {!goog.log.LogRecord}
972
*/
973
goog.log.getLogRecord = function(logger, level, msg, exception = undefined) {
974
const logRecord = goog.log.LogBuffer.getInstance().addRecord(
975
level || goog.log.Level.OFF, msg, logger.getName());
976
logRecord.setException(exception);
977
return logRecord;
978
};
979
980
981
/**
982
* Logs a goog.log.LogRecord. If the logger is currently enabled for the
983
* given message level then the given message is forwarded to all the
984
* registered output Handler objects.
985
* TODO(user): Delete this method from the public API.
986
* @param {?goog.log.Logger} logger
987
* @param {!goog.log.LogRecord} logRecord A log record to log.
988
*/
989
goog.log.publishLogRecord = function(logger, logRecord) {
990
if (goog.log.ENABLED && logger &&
991
goog.log.isLoggable(logger, logRecord.getLevel())) {
992
const loggerEntry = goog.log.LogRegistry_.getInstance().getLogRegistryEntry(
993
logger.getName());
994
loggerEntry.publish(logRecord);
995
}
996
};
997
998
999
/**
1000
* Logs a message. If the logger is currently enabled for the
1001
* given message level then the given message is forwarded to all the
1002
* registered output Handler objects.
1003
* TODO(user): The level parameter should be made required.
1004
* @param {?goog.log.Logger} logger
1005
* @param {?goog.log.Level} level One of the level identifiers.
1006
* @param {!goog.log.Loggable} msg The message to log.
1007
* @param {*=} exception An exception associated with the message.
1008
*/
1009
goog.log.log = function(logger, level, msg, exception = undefined) {
1010
if (goog.log.ENABLED && logger && goog.log.isLoggable(logger, level)) {
1011
level = level || goog.log.Level.OFF;
1012
const loggerEntry = goog.log.LogRegistry_.getInstance().getLogRegistryEntry(
1013
logger.getName());
1014
// Message callbacks can be useful when a log message is expensive to build.
1015
if (typeof msg === 'function') {
1016
msg = msg();
1017
}
1018
const logRecord = goog.log.LogBuffer.getInstance().addRecord(
1019
level, msg, logger.getName());
1020
logRecord.setException(exception);
1021
// Publish logs.
1022
loggerEntry.publish(logRecord);
1023
}
1024
};
1025
1026
1027
/**
1028
* Logs a message at the goog.log.Level.SEVERE level.
1029
* If the logger is currently enabled for the given message level then the
1030
* given message is forwarded to all the registered output Handler objects.
1031
* @param {?goog.log.Logger} logger
1032
* @param {!goog.log.Loggable} msg The message to log.
1033
* @param {*=} exception An exception associated with the message.
1034
*/
1035
goog.log.error = function(logger, msg, exception = undefined) {
1036
if (goog.log.ENABLED && logger) {
1037
goog.log.log(logger, goog.log.Level.SEVERE, msg, exception);
1038
}
1039
};
1040
1041
1042
/**
1043
* Logs a message at the goog.log.Level.WARNING level.
1044
* If the logger is currently enabled for the given message level then the
1045
* given message is forwarded to all the registered output Handler objects.
1046
* @param {?goog.log.Logger} logger
1047
* @param {!goog.log.Loggable} msg The message to log.
1048
* @param {*=} exception An exception associated with the message.
1049
*/
1050
goog.log.warning = function(logger, msg, exception = undefined) {
1051
if (goog.log.ENABLED && logger) {
1052
goog.log.log(logger, goog.log.Level.WARNING, msg, exception);
1053
}
1054
};
1055
1056
1057
/**
1058
* Logs a message at the goog.log.Level.INFO level.
1059
* If the logger is currently enabled for the given message level then the
1060
* given message is forwarded to all the registered output Handler objects.
1061
* @param {?goog.log.Logger} logger
1062
* @param {!goog.log.Loggable} msg The message to log.
1063
* @param {*=} exception An exception associated with the message.
1064
*/
1065
goog.log.info = function(logger, msg, exception = undefined) {
1066
if (goog.log.ENABLED && logger) {
1067
goog.log.log(logger, goog.log.Level.INFO, msg, exception);
1068
}
1069
};
1070
1071
1072
/**
1073
* Logs a message at the goog.log.Level.FINE level.
1074
* If the logger is currently enabled for the given message level then the
1075
* given message is forwarded to all the registered output Handler objects.
1076
* @param {?goog.log.Logger} logger
1077
* @param {!goog.log.Loggable} msg The message to log.
1078
* @param {*=} exception An exception associated with the message.
1079
*/
1080
goog.log.fine = function(logger, msg, exception = undefined) {
1081
if (goog.log.ENABLED && logger) {
1082
goog.log.log(logger, goog.log.Level.FINE, msg, exception);
1083
}
1084
};
1085
1086