forked from wesleytodd/YeoPress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.js
69 lines (61 loc) · 1.9 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var util = require('util'),
chalk = require('chalk');
// Utility extend
function extend(obj) {
var args = Array.prototype.slice.call(arguments, 1);
for (var i = 0, len = args.length; i < len; i++) {
if (args[i]) {
for (var prop in args[i]) {
obj[prop] = args[i][prop];
}
}
}
return obj;
};
// The supported severity levels
var severityLevels = ['verbose', 'log', 'warn', 'error'];
var Logger = function(options) {
if (!(this instanceof Logger)) return new Logger(options);
this.options = extend({}, Logger.defaultOptions, options);
};
Logger.defaultOptions = {
level : 'error',
verbosePrefix : '>> ',
verbosePrefixTheme : chalk.gray.bold,
verboseMessageTheme : chalk.white,
verboseStream : process.stdout,
logPrefix : '>> ',
logPrefixTheme : chalk.cyan.bold,
logMessageTheme : chalk.white,
logStream : process.stdout,
warnPrefix : '>> ',
warnPrefixTheme : chalk.yellow.bold,
warnMessageTheme : chalk.white,
warnStream : process.stdout,
errorPrefix : 'Error: ',
errorPrefixTheme : chalk.red.bold,
errorMessageTheme : chalk.red,
errorStream : process.stderr
};
severityLevels.forEach(function(fnc) {
Logger.prototype[fnc] = function(out, opts) {
if (severityLevels.indexOf(fnc) >= severityLevels.indexOf(this.options.level)) {
opts = extend({}, this.options, opts);
var prefix = opts[fnc + 'PrefixTheme'](opts[fnc + 'Prefix']);
var message;
if (typeof out === 'object') {
message = opts[fnc + 'MessageTheme'](util.inspect(out));
} else {
message = opts[fnc + 'MessageTheme'](out+'');
}
if (typeof opts[fnc + 'Stream'] === 'function') {
opts[fnc + 'Stream'].write(prefix + message);
} else if (typeof console[fnc] === 'function') {
console[fnc](prefix + message);
} else {
console.log(prefix + message);
}
}
};
});
module.exports = Logger;