-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger.js
93 lines (74 loc) · 2.28 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
'use strict';
var winston = require('winston');
var logConfig = require('../default').logger;
var transports = [];
if (logConfig.file) {
transports.push(new winston.transports.File(logConfig.file));
}
if (logConfig.console) {
transports.push(new winston.transports.Console(logConfig.console));
}
if (logConfig.http) {
transports.push(new winston.transports.Http(logConfig.http));
}
var defaultLogger = new winston.Logger({
transports: transports,
exitOnError: false
});
defaultLogger.stream = {
write: function(message, encoding) {
defaultLogger.info(message);
}
};
module.exports = function(ctx) {
var logger = new winston.Logger({
transports: transports,
exitOnError: false
});
var stringifiedCtx = function(param) {
if (ctx) {
return '[' + ctx + (param ? ':' + param : '') + '] ';
} else if (param) {
return '[' + param + '] ';
} else {
return '';
}
};
var insertCtx = function(params, additional) {
if (typeof params[0] === 'string') {
params[0] = stringifiedCtx(additional) + params[0];
} else if (typeof params[0] === 'object') {
Array.prototype.unshift.call(params, stringifiedCtx(additional));
}
return params;
};
var decorator = function(param) {
return {
error: function() {
logger.error.apply(this, insertCtx(arguments, param));
},
warn: function() {
logger.warn.apply(this, insertCtx(arguments, param));
},
info: function() {
logger.info.apply(this, insertCtx(arguments, param));
},
verbose: function() {
logger.varbose.apply(this, insertCtx(arguments, param));
},
debug: function() {
logger.debug.apply(this, insertCtx(arguments, param));
},
silly: function() {
logger.silly.apply(this, insertCtx(arguments, param));
}
};
};
var decoratorObj = decorator();
decoratorObj.start = function(param) {
var wrapper = decorator(param);
wrapper.debug('started');
return wrapper;
};
return decoratorObj;
};