forked from openstf/stf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
140 lines (117 loc) · 2.68 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* eslint quote-props:0 */
var util = require('util')
var events = require('events')
var chalk = require('chalk')
var Logger = new events.EventEmitter()
Logger.Level = {
DEBUG: 1
, VERBOSE: 2
, INFO: 3
, IMPORTANT: 4
, WARNING: 5
, ERROR: 6
, FATAL: 7
}
// Exposed for other modules
Logger.LevelLabel = {
1: 'DBG'
, 2: 'VRB'
, 3: 'INF'
, 4: 'IMP'
, 5: 'WRN'
, 6: 'ERR'
, 7: 'FTL'
}
Logger.globalIdentifier = '*'
function Log(tag) {
this.tag = tag
this.names = {
1: 'DBG'
, 2: 'VRB'
, 3: 'INF'
, 4: 'IMP'
, 5: 'WRN'
, 6: 'ERR'
, 7: 'FTL'
}
this.styles = {
1: 'grey'
, 2: 'cyan'
, 3: 'green'
, 4: 'magenta'
, 5: 'yellow'
, 6: 'red'
, 7: 'red'
}
this.localIdentifier = null
events.EventEmitter.call(this)
}
util.inherits(Log, events.EventEmitter)
Logger.createLogger = function(tag) {
return new Log(tag)
}
Logger.setGlobalIdentifier = function(identifier) {
Logger.globalIdentifier = identifier
return Logger
}
Log.Entry = function(timestamp, priority, tag, pid, identifier, message) {
this.timestamp = timestamp
this.priority = priority
this.tag = tag
this.pid = pid
this.identifier = identifier
this.message = message
}
Log.prototype.setLocalIdentifier = function(identifier) {
this.localIdentifier = identifier
}
Log.prototype.debug = function() {
this._write(this._entry(Logger.Level.DEBUG, arguments))
}
Log.prototype.verbose = function() {
this._write(this._entry(Logger.Level.VERBOSE, arguments))
}
Log.prototype.info = function() {
this._write(this._entry(Logger.Level.INFO, arguments))
}
Log.prototype.important = function() {
this._write(this._entry(Logger.Level.IMPORTANT, arguments))
}
Log.prototype.warn = function() {
this._write(this._entry(Logger.Level.WARNING, arguments))
}
Log.prototype.error = function() {
this._write(this._entry(Logger.Level.ERROR, arguments))
}
Log.prototype.fatal = function() {
this._write(this._entry(Logger.Level.FATAL, arguments))
}
Log.prototype._entry = function(priority, args) {
return new Log.Entry(
Date.now()
, priority
, this.tag
, process.pid
, this.localIdentifier || Logger.globalIdentifier
, util.format.apply(util, args)
)
}
Log.prototype._format = function(entry) {
return util.format('%s/%s %d [%s] %s'
, this._name(entry.priority)
, entry.tag
, entry.pid
, entry.identifier
, entry.message
)
}
Log.prototype._name = function(priority) {
return chalk[this.styles[priority]](this.names[priority])
}
/* eslint no-console: 0 */
Log.prototype._write = function(entry) {
console.error(this._format(entry))
this.emit('entry', entry)
Logger.emit('entry', entry)
}
exports = module.exports = Logger