forked from s-nomp/s-nomp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogUtil.js
89 lines (65 loc) · 2.01 KB
/
logUtil.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
var dateFormat = require('dateformat');
var colors = require('colors');
var severityToColor = function(severity, text) {
switch(severity) {
case 'special':
return text.cyan.underline;
case 'debug':
return text.green;
case 'warning':
return text.yellow;
case 'error':
return text.red;
default:
console.log("Unknown severity " + severity);
return text.italic;
}
};
var severityValues = {
'debug': 1,
'warning': 2,
'error': 3,
'special': 4
};
var PoolLogger = function (configuration) {
var logLevelInt = severityValues[configuration.logLevel];
var logColors = configuration.logColors;
var log = function(severity, system, component, text, subcat) {
if (severityValues[severity] < logLevelInt) return;
if (subcat){
var realText = subcat;
var realSubCat = text;
text = realText;
subcat = realSubCat;
}
var entryDesc = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss') + ' [' + system + ']\t';
if (logColors) {
entryDesc = severityToColor(severity, entryDesc);
var logString =
entryDesc +
('[' + component + '] ').italic;
if (subcat)
logString += ('(' + subcat + ') ').bold.grey;
logString += text.grey;
}
else {
var logString =
entryDesc +
'[' + component + '] ';
if (subcat)
logString += '(' + subcat + ') ';
logString += text;
}
console.log(logString);
};
// public
var _this = this;
Object.keys(severityValues).forEach(function(logType){
_this[logType] = function(){
var args = Array.prototype.slice.call(arguments, 0);
args.unshift(logType);
log.apply(this, args);
};
});
};
module.exports = PoolLogger;