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