1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const LogLevel = require('./LogLevel');
|
13 | const MethodFactory = require('./factory/MethodFactory');
|
14 | const PrefixFactory = require('./factory/PrefixFactory');
|
15 |
|
16 | const factories = Symbol('log-factories');
|
17 |
|
18 | class 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 |
|
63 | module.exports = new DefaultLogger();
|
64 |
|
65 |
|
66 | module.exports.default = module.exports;
|