1 | 'use strict';
|
2 |
|
3 |
|
4 | const LogLevel = require('./lib/LogLevel');
|
5 | const MethodFactory = require('./lib/MethodFactory');
|
6 | const PrefixFactory = require('./factory/PrefixFactory');
|
7 |
|
8 | const defaultLogger = new LogLevel({ name: 'default' });
|
9 | const cache = { default: defaultLogger };
|
10 |
|
11 |
|
12 | const existing = (typeof window !== 'undefined') ? window.log : null;
|
13 |
|
14 | module.exports = Object.assign(defaultLogger, {
|
15 |
|
16 | get factories() {
|
17 | return {
|
18 | MethodFactory,
|
19 | PrefixFactory
|
20 | };
|
21 | },
|
22 |
|
23 | get loggers() {
|
24 | return cache;
|
25 | },
|
26 |
|
27 | getLogger(options) {
|
28 | if (typeof options === 'string') {
|
29 | options = { name: options };
|
30 | }
|
31 |
|
32 | if (!options.id) {
|
33 | options.id = options.name;
|
34 | }
|
35 |
|
36 | const { name, id } = options;
|
37 | const defaults = { level: defaultLogger.level };
|
38 |
|
39 | if (typeof name !== 'string' || !name || !name.length) {
|
40 | throw new TypeError('You must supply a name when creating a logger.');
|
41 | }
|
42 |
|
43 | let logger = cache[id];
|
44 | if (!logger) {
|
45 | logger = new LogLevel(Object.assign({}, defaults, options));
|
46 | cache[id] = logger;
|
47 | }
|
48 | return logger;
|
49 | },
|
50 |
|
51 | noConflict() {
|
52 | if (typeof window !== 'undefined' && window.log === defaultLogger) {
|
53 | window.log = existing;
|
54 | }
|
55 |
|
56 | return defaultLogger;
|
57 | }
|
58 | });
|