This repository was archived by the owner on Feb 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathlog.js
More file actions
111 lines (94 loc) · 2.34 KB
/
log.js
File metadata and controls
111 lines (94 loc) · 2.34 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
/*
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': args.join(' ')});
}
return {
error: send('error'),
warn: send('warn'),
info: send('info'),
status: send('status'),
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();
this.remoteLoggers = [];
};
Log.prototype = {
_write: function(method, args, name) {
var config = util.getConfig();
var silent = config.silent;
if(silent)
return;
if(!name)
name = method.toUpperCase();
var message = moment().format('YYYY-MM-DD HH:mm:ss');
message += ' (' + name + '):\t';
var rawMessage = fmt.apply(null, args);
message += rawMessage;
if (method == 'remote')
{
// mirror remote output to info
this.output['info'](message);
_.each(this.remoteLoggers, function(logger) {
logger.logRemote(rawMessage);
});
}
else
{
this.output[method](message);
}
},
error: function() {
this._write('error', arguments);
},
warn: function() {
this._write('warn', arguments);
},
info: function() {
this._write('info', arguments);
},
remote: function() {
this._write('remote', arguments);
},
write: function() {
var args = _.toArray(arguments);
var message = fmt.apply(null, args);
this.output.info(message);
},
addRemoteLogger: function(logger) {
this.remoteLoggers.push(logger);
}
}
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.error = _.noop;
Log.prototype.write = _.noop;
Log.prototype.remote = _.noop;
}
module.exports = new Log;