forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathlog.js
51 lines (40 loc) · 1.16 KB
/
log.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var _ = require('underscore');
var sprintf = require('sprintf-js').sprintf;
var chalk = require('./chalk');
var log = {
output: _.bind(console.log, console),
level: null,
levels: {
TRACE: {value: 0, color: 'gray'},
DEBUG: {value: 1, color: 'gray'},
INFO: {value: 2, color: ''},
WARN: {value: 3, color: 'yellow'},
ERROR: {value: 4, color: 'red'}
}
};
log.setLevel = function(name) {
this.level = this.levels[name] || this.levels.INFO;
};
log.isEnabled = function(name) {
return this.level.value <= this.levels[name].value;
};
log.fail = function(e) {
log.error(sprintf('%s [%d]', (e.msg || e), (e.statusCode || 0)));
};
log.init = function() {
this.setLevel('INFO');
_.keys(this.levels).forEach(function(name) {
log[name.toLowerCase()] = function() {
var level = log.levels[name];
if (log.level.value > level.value) return;
var args = _.toArray(arguments);
if (name !== 'INFO') args.unshift('[' + name + ']');
var s = args.map(function(arg) {
return arg.toString();
}).join(' ');
if (level.color) s = chalk[level.color](s);
this.output(s);
};
});
};
module.exports = log;