UNPKG

1.6 kBJavaScriptView Raw
1/*
2 Copyright © 2018 Andrew Powell
3
4 This Source Code Form is subject to the terms of the Mozilla Public
5 License, v. 2.0. If a copy of the MPL was not distributed with this
6 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8 The above copyright notice and this permission notice shall be
9 included in all copies or substantial portions of this Source Code Form.
10*/
11
12const LogLevel = require('./LogLevel');
13const MethodFactory = require('./factory/MethodFactory');
14const PrefixFactory = require('./factory/PrefixFactory');
15
16const factories = Symbol('log-factories');
17
18class DefaultLogger extends LogLevel {
19 constructor() {
20 super({ name: 'default' });
21
22 this.cache = { default: this };
23 this[factories] = { MethodFactory, PrefixFactory };
24 }
25
26 get factories() {
27 return this[factories];
28 }
29
30 get loggers() {
31 return this.cache;
32 }
33
34 create(opts) {
35 let options;
36
37 if (typeof opts === 'string') {
38 options = { name: opts };
39 } else {
40 options = Object.assign({}, opts);
41 }
42
43 if (!options.id) {
44 options.id = options.name;
45 }
46
47 const { name, id } = options;
48 const defaults = { level: this.level };
49
50 if (typeof name !== 'string' || !name || !name.length) {
51 throw new TypeError('You must supply a name when creating a logger.');
52 }
53
54 let logger = this.cache[id];
55 if (!logger) {
56 logger = new LogLevel(Object.assign({}, defaults, options));
57 this.cache[id] = logger;
58 }
59 return logger;
60 }
61}
62
63module.exports = new DefaultLogger();
64
65// TypeScript fix
66module.exports.default = module.exports;