-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.js
108 lines (95 loc) · 3.14 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
var path = require('path');
var levels = {debug:0, info:1, warn:2, error:3, fatal:4};
var levelsStr = Object.keys(levels);
var levelLimit = 0;
var errorLimit = 3;
var useRelativePath = true;
function getLevel(level) {
var levelObj = {};
if (typeof level === 'string') {
levelObj.s = level;
levelObj.n = levels[level];
if (typeof levelObj.n === 'undefined') throw new Error('Invalid log level.');
} else if (typeof level === 'number'){
levelObj.n = level;
levelObj.s = levelsStr[level];
if (typeof levelObj.s === 'undefined') throw new Error('Invalid log level.');
} else {
throw new Error('Log level must be string or number.');
}
return levelObj;
}
function log(level, data) {
var l = getLevel(level);
if (l.n >= levelLimit) {
var out = console.log;
if (l.n >= errorLimit) out = console.error;
var msg = '[' + (new Date()).toJSON() + '] [' + [l.s.toUpperCase()] + '] [' + this.filename + '] - ' + data;
var args = Array.prototype.slice.call(arguments, 2);
args.unshift(msg);
out.apply(console, args);
if ((typeof data === 'object') && (data!==null)) {
if (data instanceof Error) console.log(data.stack);
else console.log(data);
console.log('');
}
}
};
function Logger(filename) {
this.filename = filename;
}
/** Sets the current log level. Any log request specified at lower level will not be displayed. */
Logger.prototype.setLevel = function(level) {
levelLimit = getLevel(level).n;
return this;
};
/** Gets the current log level. */
Logger.prototype.getLevel = function(cb) {
cb(levelLimit, levelsStr[levelLimit]);
return this;
};
/**
* Sets the current error level. Error level specifies a level that is considered as an error or worse.
* A log request with specified level equal to or more than the error level will be sent to STDERR
*/
Logger.prototype.setErrorThreshold = function(level) {
errorLimit = getLevel(level).n;
return this;
};
/** Gets the current error level. */
Logger.prototype.getErrorThreshold = function(cb) {
cb(errorLimit, levelsStr[errorLimit]);
return this;
};
/** @param value Set to true to make the logger print a shorter path relative to the main app. Default is true. */
Logger.prototype.useRelativePath = function(value) {
useRelativePath = value;
return this;
};
Logger.prototype.log = log;
levelsStr.forEach( function(level) {
Logger.prototype[level] = function() {
var args = Array.prototype.slice.call(arguments);
args.unshift(level);
log.apply(this, args);
};
});
var factory = module.exports = function(cModule) {
var filename = '';
if (cModule) {
if (typeof cModule == 'string') filename = cModule;
else if (cModule.filename) filename = cModule.filename;
}
if (useRelativePath) filename = path.join(path.relative(process.cwd(), path.dirname(filename)), path.basename(filename));
return new Logger(filename);
}
Object.defineProperties(factory, {
level : {
get: function() { return levelLimit; },
set: function(level) { levelLimit = getLevel(level).n; }
},
errorThreshold : {
get: function() { return errorLimit; },
set: function(level) { errorLimit = getLevel(level).n; }
},
});