-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
50 lines (42 loc) · 1.24 KB
/
logger.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
/*************
NOTE:
Consider using winston!
https://github.com/winstonjs/winston
https://medium.com/@garychambers108/better-logging-in-node-js-b3cc6fd0dafd#.d2j3lm3kh
NOTE:
**************/
var Logger = require('js-logger');
var colors = require('colors/safe');
Logger.useDefaults();
var timestampPrepender = Logger.createDefaultHandler({
formatter: function(messages, context) {
// prefix each log message with a timestamp.
var timeNow = new Date().toISOString();
timeNow = timeNow.split('T')[1].slice(0, -2);
switch (context.level.name) {
case 'DEBUG':
timeNow = colors.blue(timeNow);
break;
case 'INFO':
timeNow = colors.cyan(timeNow);
break;
case 'WARN':
timeNow = colors.yellow(timeNow);
break;
case 'ERROR':
timeNow = colors.red.inverse(timeNow);
break;
default:
timeNow = colors.cyan(timeNow);
}
messages.unshift(colors.green('[ ') + timeNow + colors.green(' ]'));
messages = colors.green(messages[0]);
}
});
Logger.setHandler(function (messages, context) {
timestampPrepender(messages, context);
});
// if (process.env.NODE_ENV === 'production') {
// Logger.setLevel(Logger.WARN);
// }
module.exports = Logger;