-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.js
188 lines (171 loc) · 3.94 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
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
const chalk = require('chalk');
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
const LOG_LEVEL = {
NONE: 0,
DEBUG: 1,
LOG: 2,
WARN: 4,
ERROR: 8,
};
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
/**
* Chalk styles for logger strings.
*/
const Styles = {
debug: chalk.reset.blue,
log: chalk.reset,
success: chalk.reset.green,
info: chalk.reset.white,
warn: chalk.reset.yellow,
error: chalk.reset.red,
};
const stylize = (label, style, args) => {
const styledLabel = style(label);
const [firstArg, ...rest] = args;
if (typeof firstArg === 'string') {
return [`${styledLabel} ${firstArg}`, ...rest];
}
return [styledLabel, ...args];
};
class Logger {
error(...args) {
console.error(...stylize('[ERROR]', Styles.error, args));
}
warn(...args) {
console.warn(...stylize('[WARNING]', Styles.warn, args));
}
log(...args) {
console.log(...args);
}
success(...args) {
console.log(...stylize('[SUCCESS]', Styles.success, args));
}
info(...args) {
console.info(...stylize('[INFO]', Styles.info, args));
}
debug(...args) {
console.debug(...stylize('[DEBUG]', Styles.log, args));
}
group(...args) {
console.group(...args);
}
groupEnd(...args) {
console.groupEnd(...args);
}
}
let currentLogger = new Logger();
let currentLogLevel = LOG_LEVEL.ERROR;
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
const setLogger = logger => {
currentLogger = logger;
};
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
const setLogLevel = level => {
switch (level) {
case LOG_LEVEL.DEBUG:
currentLogLevel =
LOG_LEVEL.DEBUG | LOG_LEVEL.LOG | LOG_LEVEL.WARN | LOG_LEVEL.ERROR;
break;
case LOG_LEVEL.LOG:
currentLogLevel = LOG_LEVEL.LOG | LOG_LEVEL.WARN | LOG_LEVEL.ERROR;
break;
case LOG_LEVEL.WARN:
currentLogLevel = LOG_LEVEL.WARN | LOG_LEVEL.ERROR;
break;
case LOG_LEVEL.ERROR:
currentLogLevel = LOG_LEVEL.ERROR;
break;
case LOG_LEVEL.NONE:
default:
currentLogLevel = LOG_LEVEL.NONE;
}
};
const shouldLog = level => {
return currentLogLevel & level;
};
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
const getLogLevel = () => {
switch (currentLogLevel) {
case LOG_LEVEL.DEBUG | LOG_LEVEL.LOG | LOG_LEVEL.WARN | LOG_LEVEL.ERROR:
return LOG_LEVEL.DEBUG;
case LOG_LEVEL.LOG | LOG_LEVEL.WARN | LOG_LEVEL.ERROR:
return LOG_LEVEL.LOG;
case LOG_LEVEL.WARN | LOG_LEVEL.ERROR:
return LOG_LEVEL.WARN;
case LOG_LEVEL.ERROR:
return LOG_LEVEL.ERROR;
default:
return LOG_LEVEL.NONE;
}
};
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
const logger = {
error(...args) {
if (shouldLog(LOG_LEVEL.ERROR)) {
currentLogger.error(...args);
}
},
warn(...args) {
if (shouldLog(LOG_LEVEL.WARN)) {
currentLogger.warn(...args);
}
},
log(...args) {
if (shouldLog(LOG_LEVEL.LOG)) {
currentLogger.log(...args);
}
},
success(...args) {
if (shouldLog(LOG_LEVEL.LOG)) {
currentLogger.success(...args);
}
},
info(...args) {
if (shouldLog(LOG_LEVEL.LOG)) {
currentLogger.info(...args);
}
},
debug(...args) {
if (shouldLog(LOG_LEVEL.DEBUG)) {
currentLogger.debug(...args);
}
},
group(...args) {
currentLogger.group(...args);
},
groupEnd(...args) {
currentLogger.groupEnd(...args);
},
};
module.exports = {
LOG_LEVEL,
Styles,
setLogger,
setLogLevel,
getLogLevel,
logger,
};