forked from misterkeebs/mkbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.js
More file actions
37 lines (29 loc) · 753 Bytes
/
Copy pathdebug.js
File metadata and controls
37 lines (29 loc) · 753 Bytes
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
const LEVELS = ['debug', 'warn', 'info', 'error'];
class Debug {
static isTest() {
return !!(process.env.npm_lifecycle_event && process.env.npm_lifecycle_event.indexOf('test') > -1);
}
static log(...s) {
if (!is(Debug.DEBUG)) return;
console.log(...s);
}
static info(...s) {
if (!is(Debug.INFO)) return;
console.info(...s);
}
static error(...s) {
if (!is(Debug.ERROR)) return;
console.error(...s);
}
}
Debug.DEBUG = 0;
Debug.WARN = 1;
Debug.INFO = 2;
Debug.ERROR = 3;
const key = Debug.isTest() ? 'TEST_LOG_LEVEL' : 'LOG_LEVEL';
function is(level) {
if (!process.env.hasOwnProperty(key)) return false;
const idx = LEVELS.indexOf(process.env[key]);
return level >= idx;
}
module.exports = Debug;