-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
139 lines (129 loc) · 3.01 KB
/
app.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const colors = require('colors');
/* eslint no-console:0 */
const LOG_LEVEL_VALUES = {
prod: -1,
error: 0,
warn: 1,
info: 2,
debug: 3,
verbose: 4
};
const DEFAULT_OPTIONS = {
logLevel: 'debug', //verbose,debug, info, error, prod
error: {
level: 'error',
force: false,
showStack: false,
showDebug: true
},
colorTheme: {
verbose: 'cyan',
info: 'green',
warn: 'yellow',
debug: 'white',
error: 'red'
}
};
let loggingOptions;
setDefault();
function init(options) {
if (options.logLevel) {
if (Object.keys(LOG_LEVEL_VALUES).indexOf(options.logLevel) !== -1) {
loggingOptions.logLevel = options.logLevel;
}
}
if (options.error) {
if (options.error.showStack) {
loggingOptions.error.showStack = !!options.error.showStack;
}
if (options.error.showDebug) {
loggingOptions.error.showDebug = !!options.error.showDebug;
}
}
if (options.colorTheme) {
for (const prop in loggingOptions.colorTheme) {
let newColor = options.colorTheme[prop];
if (newColor) {
loggingOptions.colorTheme[prop] = newColor;
}
}
colors.setTheme(loggingOptions.colorTheme);
}
}
function setDefault() {
loggingOptions = DEFAULT_OPTIONS;
colors.setTheme(loggingOptions.colorTheme);
}
function setLogLevel(logLevel) {
loggingOptions.logLevel = logLevel;
}
//region console loggers
function checkLevel(level) {
return LOG_LEVEL_VALUES[level] <= LOG_LEVEL_VALUES[loggingOptions.logLevel];
}
function write(msg, options) {
const defaultOptions = {
level: 'verbose',
stringify: false,
force: false
};
options = { ...defaultOptions, ...loggingOptions, ...options };
if (options.force || checkLevel(options.level)) {
if (options.stringify) {
msg = JSON.stringify(msg);
}
if (typeof msg === 'string') {
msg = msg[options.level];
}
console.log(msg);
}
}
function error(err, options = {}) {
options = { ...DEFAULT_OPTIONS.error, ...options };
if (options.force || checkLevel(options.level)) {
if (err.constructor.name === 'String') {
console.error(err.red);
} else {
console.error(err);
if (err.stack && options.showStack) {
console.log(err.stack.red);
}
if (err.debug && options.showDebug) {
console.log(err.debug.warn);
}
}
}
}
function debug(msg, options = {}) {
options.level = 'debug';
write(msg, options);
}
function warn(msg, options = {}) {
options.level = 'warn';
write(msg, options);
}
function info(msg, options = {}) {
options.level = 'info';
write(msg, options);
}
function verbose(msg, options = {}) {
options.level = 'verbose';
write(msg, options);
}
//endregion console loggers
module.exports = {
init: init,
setDefault: setDefault,
setLogLevel: setLogLevel,
log: debug, //debug
debug: debug, //debug
error: error, //error
warn: warn, //warn
info: info, //info
verbose: verbose, //verbose
d: debug, //debug
e: error, //error
w: warn, //warn
i: info, //info
v: verbose //verbose
};