1 |
3a515b92
|
cagy
|
'use strict';
|
2 |
|
|
|
3 |
|
|
/* global window: true */
|
4 |
|
|
/* eslint-disable
|
5 |
|
|
no-shadow,
|
6 |
|
|
no-param-reassign,
|
7 |
|
|
space-before-function-paren
|
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 |
|
|
// NOTE: this is exported so that consumers of webpack-log can use the same
|
70 |
|
|
// version of ansi-colors to decorate log messages without incurring additional
|
71 |
|
|
// dependency overhead
|
72 |
|
|
module.exports.colors = colors;
|
73 |
|
|
// NOTE: This is an undocumented function solely for the purpose of tests.
|
74 |
|
|
// Do not use this method in production code. Using in production code
|
75 |
|
|
// may result in strange behavior.
|
76 |
|
|
module.exports.delLogger = function delLogger(name) {
|
77 |
|
|
delete loglevel.loggers[name];
|
78 |
|
|
};
|
79 |
|
|
|
80 |
|
|
module.exports.factories = loglevel.factories;
|