-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlogger.js
111 lines (107 loc) · 4.01 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
"use strict";
import process from "process";
import { inspect } from "util";
import config from "./config.js";
class Logger {
constructor(moduleName='', logLevel=2) {
this.moduleName = moduleName;
this.logLevel = logLevel;
}
moduleName = '';
logLevel = 2;
toText = {
success: (...msg)=> {
if(this.logLevel < 0)
return;
return [
`\x1B[30m\x1B[47m${this.moduleName? '['+this.moduleName+']\x1B[0m ':'\x1B[0m'}\x1B[34m[${new Date().toLocaleString(undefined, {hour12: false})}]\x1B[0m `+`\x1B[32m[SUCCESS] \x1B[0m`,
...msg.map(i=>typeof(i)!='string'? inspect(i, false, null, true) : i)
].join(' ');
},
verbose: (...msg)=> {
if(this.logLevel < 3)
return;
return [
`\x1B[30m\x1B[47m${this.moduleName? '['+this.moduleName+']\x1B[0m ':'\x1B[0m'}\x1B[34m[${new Date().toLocaleString(undefined, {hour12: false})}]\x1B[0m `+`\x1B[37m[VERBOSE] \x1B[0m`,
...msg.map(i=>typeof(i)!='string'? inspect(i, false, null, true) : i)
].join(' ');
},
info: (...msg)=> {
if(this.logLevel < 2)
return;
return [
`\x1B[30m\x1B[47m${this.moduleName? '['+this.moduleName+']\x1B[0m ':'\x1B[0m'}\x1B[34m[${new Date().toLocaleString(undefined, {hour12: false})}]\x1B[0m `+`\x1B[37m[INFO] \x1B[0m`,
...msg.map(i=>typeof(i)!='string'? inspect(i, false, null, true) : i)
].join(' ');
},
warn: (...msg)=> {
if(this.logLevel < 1)
return;
return [
`\x1B[30m\x1B[47m${this.moduleName? '['+this.moduleName+']\x1B[0m ':'\x1B[0m'}\x1B[34m[${new Date().toLocaleString(undefined, {hour12: false})}]\x1B[0m `+`\x1B[33m[WARN] \x1B[0m`,
...msg.map(i=>typeof(i)!='string'? inspect(i, false, null, true) : i)
].join(' ');
},
error: (...msg)=> {
if(this.logLevel < 0)
return;
return [
`\x1B[30m\x1B[47m${this.moduleName? '['+this.moduleName+']\x1B[0m ':'\x1B[0m'}\x1B[34m[${new Date().toLocaleString(undefined, {hour12: false})}]\x1B[0m `+`\x1B[31m[ERROR] \x1B[0m`,
...msg.map(i=>typeof(i)!='string'? inspect(i, false, null, true) : i)
].join(' ');
}
};
success = (...msg)=> {
if(this.logLevel < 0)
return;
console.log(this.toText.success(...msg));
};
verbose = (...msg)=> {
if(this.logLevel < 3)
return;
console.log(this.toText.verbose(...msg));
};
info = (...msg)=> {
if(this.logLevel < 2)
return;
console.log(this.toText.info(...msg));
};
warn = (...msg)=> {
if(this.logLevel < 1)
return;
console.log(this.toText.warn(...msg));
};
error = (...msg)=> {
if(this.logLevel < 0)
return;
console.log(this.toText.error(...msg));
}
}
const logger = new Logger('BFBAN', config.logLevel);
/** @param {string} tag @param {string} message */
function prettyLogPrinter(tag, message, intent=4) {
if(intent < 0) intent = tag.length;
const padding = ' '.repeat(intent);
const ttyWidth = process.stdout.columns;
if(ttyWidth==undefined || ttyWidth<1)
return tag + message;
let paragraphs = message.split('\n');
/** @type {string[]} */
let lines = [];
lines.push(tag + paragraphs[0].slice(0, ttyWidth-tag.length%ttyWidth));
paragraphs[0] = paragraphs[0].slice(ttyWidth-tag.length%ttyWidth);
if(paragraphs[0].length == 0)
paragraphs = paragraphs.slice(1);
while(paragraphs.length) {
while(paragraphs[0].length) {
lines.push(padding+paragraphs[0].slice(0, ttyWidth-intent));
paragraphs[0] = paragraphs[0].slice(ttyWidth-intent);
}
paragraphs = paragraphs.slice(1);
}
return lines.join('\n');
}
export default logger;
export {
Logger,
};