-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogger.js
More file actions
73 lines (61 loc) · 1.61 KB
/
logger.js
File metadata and controls
73 lines (61 loc) · 1.61 KB
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
const chalk = require('chalk');
const SEVERITY = Object.freeze({ ERROR: 0, WARNING: 1, INFO: 2, DEBUG: 3 });
/**
* Log a message in a prettier way.
*
* @param {String} message Message to log to console
* @param {SEVERITY} severity Severity of the log
*/
function Log(message, severity = SEVERITY.INFO) {
/** @type {String} */
let msg = message;
/** @type {SEVERITY} */
let sev = severity;
/** @type {Function} */
let sevColor;
/** @type {String} */
let type;
if (!Object.values(SEVERITY).includes(severity)) {
sev = SEVERITY.INFO;
}
switch (sev) {
case SEVERITY.ERROR:
sevColor = chalk.bgRedBright.black;
type = ' ERROR ';
break;
case SEVERITY.WARNING:
sevColor = chalk.bgYellow.black;
type = 'WARNING';
break;
default:
case SEVERITY.INFO:
sevColor = chalk.bgBlue.white;
type = ' INFO ';
break;
case SEVERITY.DEBUG:
sevColor = chalk.bgWhite.black;
type = ' DEBUG ';
break;
}
console.log(`${TimestampString()} |${sevColor(` ${type} `)}| ${msg}`);
}
function TimestampString() {
const d = new Date();
return chalk.gray(
`${Pad(d.getUTCFullYear())}-${Pad(d.getUTCMonth())}-${Pad(d.getUTCDate())} ${Pad(d.getUTCHours())}:${Pad(d.getUTCMinutes())}:${Pad(
d.getUTCSeconds()
)}.${Pad(d.getUTCMilliseconds(), 4)}`
);
}
/**
* Pad the start of a string/number with a 0
*
* @param {String|Number} s string
* @param {Number=2} l number of chars
* @returns {String}
*/
function Pad(s, l = 2) {
return s.toString().padStart(l, '0');
}
module.exports = Log;
module.exports.SEVERITY = SEVERITY;