/
logger.js
59 lines (46 loc) · 1.21 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
var util = require('util')
function Log(tag, stream) {
this.tag = tag
this.stream = stream || process.stderr
this.levels = {
DEBUG: 'DBG'
, VERBOSE: 'VRB'
, INFO: 'INF'
, WARNING: 'WRN'
, ERROR: 'ERR'
, FATAL: 'FTL'
}
}
Log.prototype.debug = function() {
this._write(this._format(this.levels.DEBUG, arguments))
}
Log.prototype.verbose = function() {
this._write(this._format(this.levels.VERBOSE, arguments))
}
Log.prototype.info = function() {
this._write(this._format(this.levels.INFO, arguments))
}
Log.prototype.warn = function() {
this._write(this._format(this.levels.WARNING, arguments))
}
Log.prototype.error = function() {
this._write(this._format(this.levels.ERROR, arguments))
}
Log.prototype.fatal = function() {
this._write(this._format(this.levels.FATAL, arguments))
}
Log.prototype._format = function(priority, args) {
return util.format('%s %s/%s %d %s\n',
Log.prefix, priority, this.tag, process.pid, util.format.apply(util, args))
}
Log.prototype._write = function(out) {
this.stream.write(out)
}
Log.prefix = '*'
Log.createLogger = function(tag) {
return new Log(tag)
}
Log.setPrefix = function(prefix) {
Log.prefix = prefix
}
exports = module.exports = Log