-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
63 lines (60 loc) · 1.68 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
// Script modified: Fri August 14, 2020 @ 02:58:00 EDT
const colors = require('colors');
require('dotenv/config');
var enabled = {
info: (process.env.LOG_INFO == 'true'),
debug: (process.env.LOG_DEBUG == 'true'),
verbose: (process.env.LOG_VERBOSE == 'true'),
warn: (process.env.LOG_WARN == 'true'),
error: (process.env.LOG_ERROR == 'true')
};
console.log(enabled);
function log(data, level) {
try{
var string;
if (data.match(/^>/) != null) {
string = `---> ${data.slice(1)}`.dim;
} else {
var timestamp = new Date().toISOString();
string = `\n[${timestamp}][${level}] ${data}`;
}
switch (level) {
case 'info':
console.info(string.dim);
break;
case 'debug':
console.debug(string.white);
break;
case 'verbose':
console.log(string.yellow);
break;
case 'warn':
console.warn(string.yellow.underline)
break;
case 'error':
console.error(string.red.bold);
break;
}
} catch (err) {
console.log("LOGGING ERROR".red.bold.underline)
console.log(err)
}
}
var logger = {
info: (data) => {
if(enabled.info) log(data, 'info');
},
debug: (data) => {
if(enabled.debug) log(data, 'debug');
},
verbose: (data) => {
if(enabled.verbose) log(data, 'verbose');
},
warn: (data) => {
if(enabled.warn) log(data, 'warn');
},
error: (data) => {
if(enabled.error) log(data, 'error');
}
}
module.exports = logger;