-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
99 lines (89 loc) · 2.1 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
const ora = require('ora');
const chalk = require('chalk');
const spinner = ora();
class Logger {
constructor(className) {
this._debugLog = console.debug;
this._log = console.log;
this._errorLog = console.error;
this._silent = false;
this.className = className ? (className + '-') : '';
this.errorPrefix = '[' + this.className + 'E] ';
this.warnPrefix = '[' + this.className + 'W] ';
this.infoPrefix = '[' + this.className + 'I] ';
this.debugPrefix = '[' + this.className + 'D] ';
}
setSilent(silent) {
this._silent = silent;
}
error(msg) {
this._errorLog(' ' + this.errorPrefix + chalk.bold.red(msg));
}
warning(msg) {
this._log(' ' + this.warnPrefix + chalk.bold.orange(msg));
}
info(msg) {
if (this._silent) {
return;
}
if (msg instanceof Object) {
this._log(' ' + this.infoPrefix + ':');
this._log(msg);
} else {
this._log(' ' + this.infoPrefix + msg);
}
}
debug(msg) {
if (this._silent) {
return;
}
if (msg instanceof Object) {
this._debugLog(' ' + this.debugPrefix + ':');
this._debugLog(chalk.yellow(msg));
} else {
this._debugLog(' ' + this.debugPrefix + chalk.yellow(msg));
}
}
spinner() {
const self = this;
return {
start: (text) => {
if (this._silent) {
return spinner;
}
return spinner.start(text ? (self.infoPrefix + text) : undefined);
},
info: (text) => {
if (this._silent) {
return spinner;
}
return spinner.info(text ? (self.infoPrefix + text) : undefined);
},
text: (text) => {
if (this._silent) {
return spinner;
}
spinner.text = self.infoPrefix + text;
},
fail: (text) => {
if (this._silent) {
return spinner;
}
return spinner.fail(text ? (self.infoPrefix + chalk.red(text)) : undefined);
},
succeed: (text) => {
if (this._silent) {
return spinner;
}
return spinner.succeed(text ? (self.infoPrefix + chalk.green(text)) : undefined);
},
clear: () => {
if (this._silent) {
return spinner;
}
return spinner.clear();
}
}
}
}
module.exports = (className) => {return new Logger(className);};