Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
akupiec
GitHub Repository: akupiec/mad-proxy
Path: blob/master/src/config/logger.js
115 views
1
/* eslint-disable no-console */
2
const chalk = require('chalk');
3
4
const GLOB_KEY = Symbol.for('akupiec.mad-proxy.logger');
5
6
//ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF
7
const LOG_PRIORITY = ['ALL', 'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL', 'OFF'];
8
class Logger {
9
constructor() {
10
this.logLvl = 'ALL';
11
}
12
_log(lvl, ...args) {
13
let logCode = lvl;
14
const argLogIdx = LOG_PRIORITY.indexOf(this.logLvl);
15
const lvlIdx = LOG_PRIORITY.indexOf(lvl);
16
if (argLogIdx > lvlIdx) {
17
logCode = 'NO_LOG';
18
}
19
switch (logCode) {
20
case 'DEBUG': {
21
console.log(chalk.cyan(`[${lvl}]`), ...args);
22
break;
23
}
24
case 'INFO': {
25
console.log(chalk.green(`[${lvl}]`), ...args);
26
break;
27
}
28
case 'WARN': {
29
console.log(chalk.yellow(`[${lvl}] ${args[0]}`), ...args.slice(1));
30
break;
31
}
32
case 'ERROR': {
33
console.log(chalk.red(`[${lvl}] ${args[0]}`), ...args.slice(1));
34
break;
35
}
36
case 'FATAL': {
37
console.log(chalk.red('-------'));
38
console.log(chalk.red.bold.underline(`[${lvl}] ${args[0]}`), ...args.slice(1));
39
console.log(chalk.red('-------'));
40
break;
41
}
42
case 'TRACE': {
43
console.log(...args);
44
break;
45
}
46
case 'NO_LOG': {
47
break;
48
}
49
default:
50
console.log(chalk.red(`[FATAL] WRONG LOG LEVEL! CHECK CODE! [${lvl}]`, ...args));
51
}
52
}
53
54
fatal(...args) {
55
return this._log('FATAL', ...args);
56
}
57
58
error(...args) {
59
return this._log('ERROR', ...args);
60
}
61
62
warn(...args) {
63
return this._log('WARN', ...args);
64
}
65
66
info(...args) {
67
return this._log('INFO', ...args);
68
}
69
70
debug(...args) {
71
return this._log('DEBUG', ...args);
72
}
73
74
trace(...args) {
75
return this._log('TRACE', ...args);
76
}
77
}
78
79
if (!global.hasOwnProperty(GLOB_KEY)) {
80
global[GLOB_KEY] = new Logger();
81
}
82
83
module.exports = global[GLOB_KEY];
84
85