-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
189 lines (159 loc) · 3.42 KB
/
index.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const { EventEmitter } = require('events')
/**
* @typedef {('critical'|'error'|'warn'|'info'|'debug')} LogLevel
*/
/**
* @typedef {Object} LogValue
* @property {LogLevel} level
* @property {any[]} args
* @property {string[]} scopes
* @property {number} time
*/
/** @constant {LogLevel[]} */
const LEVELS = [
'critical',
'error',
'warn',
'info',
'debug'
]
Object.freeze(LEVELS)
// Its verbose for having better type inference.
const states = {
critical: false,
error: false,
warn: false,
info: false,
debug: false
}
/**
* @param {Object} obj
* @param {string} key
*/
const hasOwnProperty = (obj, key) =>
Object.prototype.hasOwnProperty.call(obj, key)
class Log extends EventEmitter {
/**
* Create a log instance
* @param {string[]} [scopes]
*/
constructor (scopes = []) {
super()
this.scopes = scopes
this.LEVELS = LEVELS
this.Log = Log
LEVELS.forEach((level) => {
this[level] = this[level].bind(this)
})
}
/**
* Create a log instance
* @param {...string} [scopes]
* @returns {Log}
*/
createLog (...scopes) {
return new Log(scopes)
}
/**
* @param {LogLevel} logLevel
*/
setLevel (logLevel) {
if (!hasOwnProperty(states, logLevel)) {
throw new Error(`Invalid LOG_LEVEL ${logLevel}.`)
}
// From which level we will log to the console?
const logLevelIndex = LEVELS.findIndex((level) => logLevel === level)
/**
* @param {LogLevel} level
* @param {number} index
*/
const updateLogLevel = (level, index) => {
states[level] = index <= logLevelIndex
}
LEVELS.forEach(updateLogLevel)
}
/**
* Check if the given log level is enabled
* @param {LogLevel} logLevel
* @returns {boolean}
*/
enabled (logLevel) {
return states[logLevel]
}
/**
* @param {('log')} evtName
* @param {(logValue: LogValue) => void} cb
*/
on (evtName, cb) {
return super.on(evtName, cb)
}
/**
* @private
* @param {LogLevel} level
* @param {...any} args
*/
_log (level, ...args) {
if (!states[level]) return
/** @type {LogValue} */
const logValue = {
level,
args,
scopes: [...this.scopes],
time: Date.now()
}
baseLogger.emit('log', logValue)
}
/**
* @param {...any} args
*/
critical (...args) {
return this._log('critical', ...args)
}
/**
* @param {...any} args
*/
error (...args) {
return this._log('error', ...args)
}
/**
* @param {...any} args
*/
warn (...args) {
return this._log('warn', ...args)
}
/**
* @param {...any} args
*/
info (...args) {
return this._log('info', ...args)
}
/**
* @param {...any} args
*/
debug (...args) {
return this._log('debug', ...args)
}
}
const baseLogger = new Log()
const consoleFnMap = {
critical: 'error',
error: 'error',
warn: 'warn',
info: 'log',
debug: 'debug'
}
function consoleLogger ({ level, scopes, args }) {
const scope = scopes.length > 0 ? `[${scopes.join('][')}]` : ''
const prefix = `[${level.toUpperCase()}]${scope}`
console[consoleFnMap[level]](prefix, ...args)
}
baseLogger.disableDefaultConsoleLogger = () => {
baseLogger.removeListener('log', consoleLogger)
}
baseLogger.enableDefaultConsoleLogger = () => {
if (!baseLogger.listeners('log').includes(consoleLogger)) {
baseLogger.on('log', consoleLogger)
}
}
baseLogger.enableDefaultConsoleLogger()
module.exports = baseLogger