-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlog.js
50 lines (40 loc) · 1.25 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
'use strict';
var bunyan = require('bunyan');
var cls = require('continuation-local-storage');
var logContext = cls.getNamespace('app-log-ctx');
function decorateLogMethod(logMethod) {
return function () {
var args = Array.prototype.slice.call(arguments); // clone
args.unshift({
requestId: logContext.get('requestId'),
sessionId: logContext.get('sessionId')
});
return logMethod.apply(undefined, args);
};
}
function decorateChildMethod(childCreator) {
return function () {
return new Logger(childCreator.apply(undefined, arguments));
};
}
function Logger(bunyanLogger) {
for (var key in bunyanLogger) {
var attribute = bunyanLogger[key];
if (typeof attribute === 'function') {
var originalMethod = attribute.bind(bunyanLogger);
if (['trace', 'debug', 'info', 'warn', 'error', 'fatal'].indexOf(key) !== -1) {
this[key] = decorateLogMethod(originalMethod);
} else if (key === 'child') {
this[key] = decorateChildMethod(originalMethod);
} else {
this[key] = originalMethod;
}
} else {
this[key] = attribute;
}
}
}
function createLogger(name) {
return new Logger(bunyan.createLogger({ name: name }));
}
module.exports.createLogger = createLogger;