-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
380 lines (326 loc) · 9.91 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
const CONFIG = {
SYSTEM: {
reset: "\x1b[0m",
bold: "\x1b[1m",
dim: "\x1b[2m",
italic: "\x1b[3m",
underscore: "\x1b[4m",
reverse: "\x1b[7m",
strikethrough: "\x1b[9m",
backoneline: "\x1b[1A",
cleanthisline: "\x1b[K"
},
FONT: {
black: "\x1b[30m",
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
magenta: "\x1b[35m",
cyan: "\x1b[36m",
white: "\x1b[37m",
},
BACKGROUND: {
black: "\x1b[40m",
red: "\x1b[41m",
green: "\x1b[42m",
yellow: "\x1b[43m",
blue: "\x1b[44m",
magenta: "\x1b[45m",
cyan: "\x1b[46m",
white: "\x1b[47m"
}
};
// Sequence of levels is important.
const LEVELS = ["success", "debug", "info", "warn", "error", "disable"];
class Logger {
constructor(name) {
// Current command
this.command = '';
// Last line
this.lastCommand = '';
this.name = name || ""
// set level from env
const level = typeof process !== 'undefined' ? process.env.LOGGER : undefined;
if (this.isLevelValid(level)) {
this.level = level;
}
this.noColor = false;
this._getDate = () => (new Date()).toISOString();
this._customizedConsole = console;
}
createNamedLogger(name) {
return new Logger(name)
}
setLevel(level) {
if (this.isLevelValid(level)) {
this.level = level;
} else {
throw "Level you are trying to set is invalid";
}
}
setLogStream(newStream) {
if (newStream.writable) {
this._customizedConsole = new console.Console(newStream);
} else {
throw "invalid writable stream object";
}
}
setLevelNoColor() {
this.noColor = true;
}
setLevelColor() {
this.noColor = false;
}
isLevelValid(level) {
return LEVELS.includes(level);
}
isAllowedLevel(level) {
return this.level ? LEVELS.indexOf(this.level) <= LEVELS.indexOf(level) : true
}
log(...args) {
this.append(...args);
if (!this.noColor) {
this.command += CONFIG.SYSTEM.reset;
}
this._print(this.command);
// Save last command if we need to use for joint
this.lastCommand = this.command;
this.command = '';
return this;
}
// deprecated
joint() {
console.error("node-color-log warning: `joint` is deprecated, please use `append`");
// Clear the last line
this._print(CONFIG.SYSTEM.backoneline + CONFIG.SYSTEM.cleanthisline);
// Reset the command to let it joint the next
// And print from the position of last line
this.command = '';
// if joint more than twice, we should clean the previous
// backline command, since we should only do it for the
// current time.
this.lastCommand = this.lastCommand.replace(CONFIG.SYSTEM.backoneline, "");
// back to the last line
this.command += CONFIG.SYSTEM.backoneline;
this.command += this.lastCommand;
return this;
}
setDate(callback) {
this._getDate = callback;
}
getPrefix() {
if (this.name) {
return `${this._getDate()} [${this.name}]`;
} else {
return this._getDate();
}
}
color(ticket) {
if (ticket in CONFIG.FONT) {
this.command += CONFIG.FONT[ticket];
} else {
console.error("node-color-log warning: Font color not found! Use the default.")
}
return this;
}
bgColor(ticket) {
if (ticket in CONFIG.BACKGROUND) {
this.command += CONFIG.BACKGROUND[ticket];
} else {
console.error("node-color-log warning: Background color not found! Use the default.")
}
return this;
}
bold() {
this.command += CONFIG.SYSTEM.bold;
return this;
}
dim() {
this.command += CONFIG.SYSTEM.dim;
return this;
}
underscore() {
this.command += CONFIG.SYSTEM.underscore;
return this;
}
strikethrough() {
this.command += CONFIG.SYSTEM.strikethrough;
return this;
}
reverse() {
this.command += CONFIG.SYSTEM.reverse;
return this;
}
italic() {
this.command += CONFIG.SYSTEM.italic;
return this;
}
fontColorLog(ticket, text, setting) {
let command = '';
if (setting) {
command += this.checkSetting(setting);
}
if (ticket in CONFIG.FONT) {
command += CONFIG.FONT[ticket];
} else {
console.error("node-color-log warning: Font color not found! Use the default.")
}
command += text;
command += CONFIG.SYSTEM.reset;
this.lastCommand = command;
this._print(command);
}
bgColorLog(ticket, text, setting) {
let command = '';
if (setting) {
command += this.checkSetting(setting);
}
if (ticket in CONFIG.BACKGROUND) {
command += CONFIG.BACKGROUND[ticket];
} else {
console.error("node-color-log warning: Background color not found! Use the default.")
}
command += text;
command += CONFIG.SYSTEM.reset;
this.lastCommand = command;
this._print(command);
}
colorLog(ticketObj, text, setting) {
let command = '';
if (setting) {
command += this.checkSetting(setting);
}
if (ticketObj.font in CONFIG.FONT) {
command += CONFIG.FONT[ticketObj.font];
} else {
console.error("node-color-log warning: Font color not found! Use the default.")
}
if (ticketObj.bg in CONFIG.BACKGROUND) {
command += CONFIG.BACKGROUND[ticketObj.bg]
} else {
console.error("node-color-log warning: Background color not found! Use the default.")
}
command += text;
command += CONFIG.SYSTEM.reset;
this.lastCommand = command;
this._print(command);
}
error(...args) {
if (!this.isAllowedLevel("error"))
return;
if (this.noColor) {
const d = this.getPrefix();
this.log(d, " [ERROR] ", ...args);
} else {
const d = this.getPrefix();
this.append(d + " ")
.bgColor('red').append('[ERROR]').reset()
.append(" ")
.color('red').log(...args);
}
}
warn(...args) {
if (!this.isAllowedLevel("warn"))
return;
if (this.noColor) {
const d = this.getPrefix();
this.log(d, " [WARN] ", ...args);
} else {
const d = this.getPrefix();
this.append(d + " ")
.bgColor('yellow').color('black').append('[WARN]').reset()
.append(" ")
.color('yellow').log(...args);
}
}
info(...args) {
if (!this.isAllowedLevel("info"))
return;
if (this.noColor) {
const d = this.getPrefix();
this.log(d, " [INFO] ", ...args);
} else {
const d = this.getPrefix();
this.append(d + " ")
.bgColor('green').color('black').append('[INFO]').reset()
.append(" ")
.color('green').log(...args);
}
}
debug(...args) {
if (!this.isAllowedLevel("debug"))
return;
if (this.noColor) {
const d = this.getPrefix();
this.log(d, " [DEBUG] ", ...args);
} else {
const d = this.getPrefix();
this.append(d + " ")
.bgColor('cyan').color('black').append("[DEBUG]").reset()
.append(' ')
.color('cyan')
.log(...args);
}
}
success(...args) {
if (!this.isAllowedLevel("success"))
return;
if (this.noColor) {
const d = this.getPrefix();
this.log(d, " [SUCCESS] ", ...args);
} else {
const d = this.getPrefix();
this.append(d + " ")
.bgColor('green').color('black').append("[SUCCESS]").reset()
.append(' ')
.color('green')
.log(...args);
}
}
checkSetting(setting) {
const validSetting = ['bold', 'italic', 'dim', 'underscore', 'reverse', 'strikethrough'];
let command = '';
for (const item in setting) {
if (validSetting.indexOf(item) !== -1) {
if (setting[item] === true) {
command += CONFIG.SYSTEM[item];
} else if (setting[item] !== false) {
console.error(`node-color-log warning: The value ${item} should be boolean.`)
}
} else {
console.error(`node-color-log warning: ${item} is not valid in setting.`)
}
}
return command;
}
// helper function to output the the log to stream
_print(...args) {
this._customizedConsole.error(...args);
}
// helper function to append the command buffer
append(...args) {
for (const idx in args) {
const arg = args[idx];
if (typeof arg === "string") {
this.command += arg;
} else {
try {
this.command += JSON.stringify(arg);
} catch {
this.command += arg;
}
}
if (args.length > 1 && idx < args.length - 1) {
this.command += " ";
}
}
return this;
}
reset() {
this.command += CONFIG.SYSTEM.reset;
return this;
}
}
const logger = new Logger();
module.exports = logger;