-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.js
362 lines (334 loc) · 10.2 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
'use strict';
const async = require('./lib/async');
const ip = require('./lib/ip');
const msg = require('./lib/msg');
const file = require('./lib/file');
const remote = require('./lib/remote');
const Table = require('./lib/table');
const today = require('./lib/today');
const buff = require('./buffer');
const EventEmitter = require('events').EventEmitter;
const events = new EventEmitter();
// a list of logger objects for auto flush
const loggers = [];
var address = null;
// default is 5 seconds
var autoFlushInterval = 5000;
var configData;
module.exports.setup = function (config) {
configData = config || { level: {} };
configData.stackdriver = config.stackdriver || false;
ip.setup();
address = ip.get();
msg.setup(configData);
file.setup(configData.level, configData.file, configData.oneFile);
remote.setup(configData.remote);
today.setup(configData.rotationType, configData.useTimestamp);
buff.setup(configData.bufferSize);
if (configData.bufferFlushInterval) {
autoFlushInterval = configData.bufferFlushInterval;
}
if (!configData.remote && !configData.file) {
return;
}
// auto flush log data at every x milliseconds
module.exports._timerFlush();
};
module.exports.events = events;
module.exports.updatePrefix = function (prefix) {
for (var i = 0, len = loggers.length; i < len; i++) {
loggers[i].prefix = prefix;
}
};
module.exports.create = function (prefix, name, filepath) {
var logger = new Logger(prefix, name, filepath);
loggers.push(logger);
return logger;
};
module.exports.clean = function (cb) {
var tasks = [
module.exports.forceFlush,
file.destroyAllStreams
];
async.series(tasks, cb);
};
module.exports.forceFlush = function (cb) {
async.each(loggers, function (logger, next) {
logger._autoFlush(next);
}, cb);
};
module.exports._timerFlush = function () {
// auto flush buffered log data at x miliseconds
// Node.js timer implementation should be effecient for handling lots of timers
// https://github.com/joyent/node/blob/master/deps/uv/src/unix/timer.c #120
setTimeout(function () {
module.exports.forceFlush(module.exports._timerFlush);
}, autoFlushInterval);
};
function Logger(prefix, name, filepath, config) {
this.prefix = prefix;
this.name = name;
this.filepath = filepath || 'unknown';
this.config = configData || {};
}
Logger.prototype.createTable = function (obj) {
var table = new Table(obj);
return table.get();
};
Logger.prototype.verbose = function () {
// check enabled or not
if (!this.config.level['verbose']) {
// not enabled
return;
}
var messages = [];
for (var i = 0, len = arguments.length; i < len; i++) {
messages.push(arguments[i]);
}
this._handleLog.apply(this, ['verbose', messages]);
};
Logger.prototype.sys = function () {
// check enabled or not
if (!this.config.level['sys']) {
// not enabled
return;
}
var messages = [];
for (var i = 0, len = arguments.length; i < len; i++) {
messages.push(arguments[i]);
}
this._handleLog.apply(this, ['sys', messages]);
};
Logger.prototype.debug = function () {
// check enabled or not
if (!this.config.level['debug']) {
// not enabled
return;
}
var messages = [];
for (var i = 0, len = arguments.length; i < len; i++) {
messages.push(arguments[i]);
}
this._handleLog.apply(this, ['debug', messages]);
};
Logger.prototype.table = function () {
// check enabled or not
if (!this.config.level['table']) {
// not enabled
return;
}
var messages = [];
for (var i = 0, len = arguments.length; i < len; i++) {
messages.push(arguments[i]);
}
this._handleLog.apply(this, ['table', messages]);
};
Logger.prototype.trace = function () {
// check enabled or not
if (!this.config.level['trace']) {
// not enabled
return;
}
var messages = [];
for (var i = 0, len = arguments.length; i < len; i++) {
messages.push(arguments[i]);
}
var traceError = new Error('<stack trace>');
var trace = traceError.stack.replace('Error: ', '');
this._handleLog.apply(this, ['trace', messages]);
this._handleLog('trace', [trace]);
};
Logger.prototype.info = function () {
// check enabled or not
if (!this.config.level['info']) {
// not enabled
return;
}
var messages = [];
for (var i = 0, len = arguments.length; i < len; i++) {
messages.push(arguments[i]);
}
this._handleLog.apply(this, ['info', messages]);
};
Logger.prototype.warning = function () {
// check enabled or not
if (!this.config.level['warn']) {
// not enabled
return;
}
var messages = [];
for (var i = 0, len = arguments.length; i < len; i++) {
messages.push(arguments[i]);
}
this._handleLog.apply(this, ['warn', messages]);
};
// alias of warning
Logger.prototype.warn = function () {
// check enabled or not
if (!this.config.level['warn']) {
// not enabled
return;
}
var messages = [];
for (var i = 0, len = arguments.length; i < len; i++) {
messages.push(arguments[i]);
}
this._handleLog.apply(this, ['warn', messages]);
};
Logger.prototype.error = function () {
// check enabled or not
if (!this.config.level['error']) {
// not enabled
return;
}
var handled = _handleOptionalFmt(this, 'error', arguments);
if (handled) {
return;
}
var messages = [];
for (var i = 0, len = arguments.length; i < len; i++) {
messages.push(arguments[i]);
}
this._handleLog.apply(this, ['error', messages]);
};
Logger.prototype.fatal = function () {
// check enabled or not
if (!this.config.level['fatal']) {
// not enabled
return;
}
var handled = _handleOptionalFmt(this, 'fatal', arguments);
if (handled) {
return;
}
var messages = [];
for (var i = 0, len = arguments.length; i < len; i++) {
messages.push(arguments[i]);
}
this._handleLog.apply(this, ['fatal', messages]);
};
function _handleOptionalFmt(that, level, args) {
if (that.config.stackdriver) {
// stackdriver specific formatting....
// https://cloud.google.com/error-reporting/docs/formatting-error-messages
var contexts = [];
var errStack = '';
for (var j = 0, jen = args.length; j < jen; j++) {
if (args[j] instanceof Error) {
errStack += args[j].stack;
continue;
}
contexts.push(args[j]);
}
var formatted = JSON.stringify({
eventTime: new Date(new Date().toUTCString()),
serviceContext: { service: that.prefix + ':' + that.name },
message: errStack,
context: {
message: contexts.join(' '),
reportLocation: {
filePath: that.filepath,
lineNumber: 0,
functionName: 'unknown'
}
}
});
that._handleLog.apply(that, [level, [formatted]]);
return true;
}
return false;
}
Logger.prototype._handleLog = function (levelName, message) {
// table is the same as debug
if (levelName === 'table') {
levelName = 'debug';
var list = [];
for (var i in message) {
var table = new Table(message[i]);
list.push(table.get());
}
message = list;
}
// if there is no config
if (!this.config || !this.config.level) {
// no configurations for log module at all -> fall back to console
if (levelName === 'error' || levelName === 'fatal') {
console.error.apply(console, message);
}
return;
}
var logMsg = msg.create(this.prefix, this.name, levelName, message);
// if console is enabled, we output to console
if (this.config.console) {
switch (levelName) {
case 'error':
case 'fatal':
console.error(logMsg.message);
break;
case 'warn':
case 'warning':
console.warn(logMsg.message);
break;
default:
console.log(logMsg.message);
break;
}
}
// add log message to buffer. buffer will flush overflowed log message
var bufferedMsg = buff.add(levelName, logMsg);
if (bufferedMsg) {
// this log level is enabled and there is flushed out log data
this._outputLog(levelName, bufferedMsg);
}
};
Logger.prototype._outputLog = function (levelName, bufferedMsg) {
if (this.config.file) {
// we pass bufferedMsg object instead of stringified bufferedMsg.messages
// to make sure the log rotation is done accurately
file.log(levelName, bufferedMsg);
}
if (this.config.remote) {
remote.log(levelName, bufferedMsg);
}
var msges = bufferedMsg.filter(function (item) {
return item.msg;
});
var times = bufferedMsg.filter(function (item) {
return item.time;
});
events.emit('output', address, this.name, levelName, { messages: msges, timestamps: times });
};
Logger.prototype._autoFlush = function (cb) {
// if there is no config -> we output nothing
if (!this.config || !this.config.level) {
return cb();
}
var that = this;
var flushed = buff.flushAll();
var list = Object.keys(flushed);
async.each(list, function (level, callback) {
// check enabled or not
if (!that.config.level[level]) {
// not enabled
return callback();
}
if (!flushed[level]) {
return callback();
}
var data = flushed[level];
var fileLog = function (moveOn) {
if (that.config.file) {
return file.log(level, data, moveOn);
}
moveOn();
};
var remoteLog = function (moveOn) {
if (that.config.remote) {
return remote.log(level, data.messages.join('\n'), moveOn);
}
moveOn();
};
events.emit('output', address, that.name, level, data);
async.series([fileLog, remoteLog], callback);
}, cb);
};