-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
116 lines (93 loc) · 2.35 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict';
var util = require('util');
var chalk = require('chalk');
var log = module.exports = {};
var levels = {
debug: 1,
info: 2,
warn: 3,
error: 4
};
var colors = {
debug: 'blue',
info: 'cyan',
warn: 'yellow',
error: 'red'
};
log.quiet = false;
log.width = 15;
var SPM_LOG = process.env.SPM_LOG;
if (SPM_LOG && levels[SPM_LOG.toLowerCase()]) {
log.level = SPM_LOG.toLowerCase();
} else {
log.level = 'info';
}
log.log = function(level, msg) {
if (levels[level] >= levels[log.level] && log.quiet === false) {
if (console[level]) {
console[level](msg);
}
else {
console.log(msg);
}
}
};
log.debug = function() {
var category = arguments[0];
var args = Array.prototype.slice.call(arguments).slice(1);
var msg = util.format.apply(this, args);
log.log('debug', getMsg(category, msg, 'debug'));
};
log.info = function() {
var category = arguments[0];
var args = Array.prototype.slice.call(arguments).slice(1);
var msg = util.format.apply(this, args);
log.log('info', getMsg(category, msg, 'info'));
};
log.warn = function() {
var category = arguments[0];
var args = Array.prototype.slice.call(arguments).slice(1);
var msg = util.format.apply(this, args);
log.log('warn', getMsg(category, msg, 'warn'));
};
log.error = function() {
var category = arguments[0];
var args = Array.prototype.slice.call(arguments).slice(1);
args = args.map(function(arg) {
if (arg.message) {
return arg.message;
}
if (arg.code) {
return arg.code;
}
return arg;
});
var msg = util.format.apply(this, args);
log.log('error', getMsg(category, msg, 'error'));
};
log.config = function(options) {
if (options.verbose) {
log.level = 'debug';
}
if (options.quiet) {
log.level = 'warn';
}
var colorOption = options.color;
if (colorOption && typeof colorOption === 'object') {
for (var level in colorOption) {
colors[level] = colorOption[level];
}
}
chalk.enabled = colorOption !== false;
};
function getMsg(category, msg, level) {
var len = Math.max(0, log.width - category.length);
var pad = new Array(len + 1).join(' ');
var fn = chalk[colors[level]];
if (typeof fn === 'function') {
category = fn(category);
}
msg = msg.replace(process.cwd(), '$CWD')
.replace(process.env.HOME, '~');
return pad + category + ': ' + msg;
}