-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
121 lines (104 loc) · 2.8 KB
/
logger.js
File metadata and controls
121 lines (104 loc) · 2.8 KB
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
117
118
119
120
121
/**
* Thanks to Caolan McMahon's work on Jam on which this file is based.
* https://github.com/caolan/jam/
*/
var util = require('util'),
colors = require('colors');
/**
* The level to log at, change this to alter the global logging level.
* Possible options are: error, warning, info, debug. Default level is info.
*/
exports.level = 'info';
/**
* Executes a function only if the current log level is in the levels list
*
* @param {Array} levels
* @param {Function} fn
*/
var forLevels = function (levels, fn) {
return function (label, val) {
for (var i = 0; i < levels.length; i++) {
if (levels[i] === exports.level) {
return fn(label, val);
}
}
};
};
/**
* Logs debug messages, using util.inspect to show the properties of objects
* (logged for 'debug' level only)
*/
exports.debug = forLevels(['debug'], function (label, val) {
if (val === undefined) {
val = label;
label = null;
}
if (typeof val !== 'string') {
val = util.inspect(val);
}
if (label && val) {
console.log(label.magenta + ' ' + val);
}
else {
console.log(label);
}
});
/**
* Logs info messages (logged for 'info' and 'debug' levels)
*/
exports.info = forLevels(['info', 'debug'], function (label, val) {
if (val === undefined) {
val = label;
label = null;
}
if (typeof val !== 'string') {
val = util.inspect(val);
}
if (label) {
console.log(label.cyan + ' ' + val);
}
else {
console.log(val);
}
});
/**
* Logs warnings messages (logged for 'warning', 'info' and 'debug' levels)
*/
exports.warning = forLevels(['warning', 'info', 'debug'], function (msg) {
console.log(('Warning: '.bold + msg).yellow);
});
/**
* Logs error messages (always logged)
*/
exports.error = function (err) {
var msg = err.message || err.error || err;
if (err.stack) {
msg = err.stack.replace(/^Error: /, '');
}
console.log(('Error: '.bold + msg).red);
};
/**
* Display a failure message if exit is unexpected.
*/
exports.clean_exit = false;
exports.end = function (msg) {
exports.clean_exit = true;
exports.success(msg);
};
exports.success = function (msg) {
//console.log(('\n' +'OK'.bold + (msg ? ': '.bold + msg: '')).green);
};
var _onExit = function () {
if (!exports.clean_exit) {
//console.log('\n' +'Failed'.bold.red);
process.removeListener('exit', _onExit);
process.exit(1);
}
};
process.on('exit', _onExit);
/**
* Log uncaught exceptions in the same style as normal errors.
*/
process.on('uncaughtException', function (err) {
exports.error(err.stack || err);
});