1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | const uuid = require('uuid/v4');
|
10 | const colors = require('ansi-colors');
|
11 | const loglevel = require('./loglevel');
|
12 |
|
13 | const symbols = {
|
14 | trace: colors.grey('₸'),
|
15 | debug: colors.cyan('➤'),
|
16 | info: colors.blue(colors.symbols.info),
|
17 | warn: colors.yellow(colors.symbols.warning),
|
18 | error: colors.red(colors.symbols.cross)
|
19 | };
|
20 |
|
21 | const defaults = {
|
22 | name: '<unknown>',
|
23 | level: 'info',
|
24 | unique: true
|
25 | };
|
26 |
|
27 | const prefix = {
|
28 | level (options) {
|
29 | return symbols[options.level];
|
30 | },
|
31 | template: `{{level}} ${colors.gray('「{{name}}」')}: `
|
32 | };
|
33 |
|
34 | function log (options) {
|
35 | const opts = Object.assign({}, defaults, options);
|
36 | const { id } = options;
|
37 |
|
38 | opts.prefix = Object.assign({}, prefix, options.prefix);
|
39 | delete opts.id;
|
40 |
|
41 | Object.defineProperty(opts, 'id', {
|
42 | get() {
|
43 | if (!id) {
|
44 | return this.name + (opts.unique ? `-${uuid()}` : '');
|
45 | }
|
46 |
|
47 | return id;
|
48 | }
|
49 | });
|
50 |
|
51 | if (opts.timestamp) {
|
52 | opts.prefix.template = `[{{time}}] ${opts.prefix.template}`;
|
53 | }
|
54 |
|
55 | const log = loglevel.getLogger(opts);
|
56 |
|
57 | if (!Object.prototype.hasOwnProperty.call(log, 'id')) {
|
58 | Object.defineProperty(log, 'id', {
|
59 | get() {
|
60 | return opts.id;
|
61 | }
|
62 | });
|
63 | }
|
64 |
|
65 | return log;
|
66 | }
|
67 |
|
68 | module.exports = log;
|
69 |
|
70 |
|
71 |
|
72 | module.exports.colors = colors;
|
73 |
|
74 |
|
75 |
|
76 | module.exports.delLogger = function delLogger(name) {
|
77 | delete loglevel.loggers[name];
|
78 | };
|
79 |
|
80 | module.exports.factories = loglevel.factories;
|