UNPKG

1.47 kBJavaScriptView Raw
1'use strict';
2
3require('object.assign/shim')();
4require('es6-symbol/implement');
5
6/* global window: true */
7const LogLevel = require('./lib/LogLevel');
8const MethodFactory = require('./lib/MethodFactory');
9const PrefixFactory = require('./factory/PrefixFactory');
10
11const defaultLogger = new LogLevel({ name: 'default' });
12const cache = { default: defaultLogger };
13
14// Grab the current global log variable in case of overwrite
15const existing = (typeof window !== 'undefined') ? window.log : null;
16
17module.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});
62
63// TypeScript fix
64module.exports.default = module.exports;