liusuyi
2023-04-24 4737f1e038743ced243c9e52423404d9034d6107
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
 
/* global window: true */
/* eslint-disable
  no-shadow,
  no-param-reassign,
  space-before-function-paren
*/
const LogLevel = require('./LogLevel');
const MethodFactory = require('./MethodFactory');
const PrefixFactory = require('./PrefixFactory');
 
const defaultLogger = new LogLevel({ name: 'default' });
const cache = { default: defaultLogger };
 
// Grab the current global log variable in case of overwrite
const existing = (typeof window !== 'undefined') ? window.log : null;
 
const loglevel = Object.assign(defaultLogger, {
  get factories() {
    return {
      MethodFactory,
      PrefixFactory
    };
  },
  get loggers() {
    return cache;
  },
  getLogger(options) {
    if (typeof options === 'string') {
      options = { name: options };
    }
 
    if (!options.id) {
      options.id = options.name;
    }
 
    const { name, id } = options;
    const defaults = { level: defaultLogger.level };
 
    if (typeof name !== 'string' || !name || !name.length) {
      throw new TypeError('You must supply a name when creating a logger');
    }
 
    let logger = cache[id];
 
    if (!logger) {
      logger = new LogLevel(Object.assign({}, defaults, options));
 
      cache[id] = logger;
    }
 
    return logger;
  },
  noConflict() {
    if (typeof window !== 'undefined' && window.log === defaultLogger) {
      window.log = existing;
    }
 
    return defaultLogger;
  }
});
 
module.exports = loglevel;