Last change
on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.4 KB
|
Line | |
---|
1 | 'use strict';
|
---|
2 |
|
---|
3 | /* global window: true */
|
---|
4 | /* eslint-disable
|
---|
5 | no-shadow,
|
---|
6 | no-param-reassign,
|
---|
7 | space-before-function-paren
|
---|
8 | */
|
---|
9 | const LogLevel = require('./LogLevel');
|
---|
10 | const MethodFactory = require('./MethodFactory');
|
---|
11 | const PrefixFactory = require('./PrefixFactory');
|
---|
12 |
|
---|
13 | const defaultLogger = new LogLevel({ name: 'default' });
|
---|
14 | const cache = { default: defaultLogger };
|
---|
15 |
|
---|
16 | // Grab the current global log variable in case of overwrite
|
---|
17 | const existing = (typeof window !== 'undefined') ? window.log : null;
|
---|
18 |
|
---|
19 | const loglevel = Object.assign(defaultLogger, {
|
---|
20 | get factories() {
|
---|
21 | return {
|
---|
22 | MethodFactory,
|
---|
23 | PrefixFactory
|
---|
24 | };
|
---|
25 | },
|
---|
26 | get loggers() {
|
---|
27 | return cache;
|
---|
28 | },
|
---|
29 | getLogger(options) {
|
---|
30 | if (typeof options === 'string') {
|
---|
31 | options = { name: options };
|
---|
32 | }
|
---|
33 |
|
---|
34 | if (!options.id) {
|
---|
35 | options.id = options.name;
|
---|
36 | }
|
---|
37 |
|
---|
38 | const { name, id } = options;
|
---|
39 | const defaults = { level: defaultLogger.level };
|
---|
40 |
|
---|
41 | if (typeof name !== 'string' || !name || !name.length) {
|
---|
42 | throw new TypeError('You must supply a name when creating a logger');
|
---|
43 | }
|
---|
44 |
|
---|
45 | let logger = cache[id];
|
---|
46 |
|
---|
47 | if (!logger) {
|
---|
48 | logger = new LogLevel(Object.assign({}, defaults, options));
|
---|
49 |
|
---|
50 | cache[id] = logger;
|
---|
51 | }
|
---|
52 |
|
---|
53 | return logger;
|
---|
54 | },
|
---|
55 | noConflict() {
|
---|
56 | if (typeof window !== 'undefined' && window.log === defaultLogger) {
|
---|
57 | window.log = existing;
|
---|
58 | }
|
---|
59 |
|
---|
60 | return defaultLogger;
|
---|
61 | }
|
---|
62 | });
|
---|
63 |
|
---|
64 | module.exports = loglevel;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.