forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.js
81 lines (68 loc) · 1.73 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
70
71
72
73
74
75
76
77
78
79
80
81
/*
Lightweight logger, print everything that is send to error, warn
and messages to stdout (the terminal). If config.debug is set in config
also print out everything send to debug.
*/
var moment = require('moment');
var fmt = require('util').format;
var _ = require('lodash');
var util = require('./util');
var config = util.getConfig();
var debug = config.debug;
var silent = config.silent;
var sendToParent = function() {
var send = method => (...args) => {
process.send({log: method, message: args.join(' ')});
}
return {
error: send('error'),
warn: send('warn'),
info: send('info'),
write: send('write')
}
}
var Log = function() {
_.bindAll(this);
this.env = util.gekkoEnv();
if(this.env === 'standalone')
this.output = console;
else if(this.env === 'child-process')
this.output = sendToParent();
};
Log.prototype = {
_write: function(method, args, name) {
if(!name)
name = method.toUpperCase();
var message = moment().format('YYYY-MM-DD HH:mm:ss');
message += ' (' + name + '):\t';
message += fmt.apply(null, args);
this.output[method](message);
},
error: function() {
this._write('error', arguments);
},
warn: function() {
this._write('warn', arguments);
},
info: function() {
this._write('info', arguments);
},
write: function() {
var args = _.toArray(arguments);
var message = fmt.apply(null, args);
this.output.info(message);
}
}
if(debug)
Log.prototype.debug = function() {
this._write('info', arguments, 'DEBUG');
}
else
Log.prototype.debug = _.noop;
if(silent) {
Log.prototype.debug = _.noop;
Log.prototype.info = _.noop;
Log.prototype.warn = _.noop;
Log.prototype.write = _.noop;
}
module.exports = new Log;