-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
executable file
·73 lines (61 loc) · 1.35 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
// Get external dependencies
const log4js = require('log4js');
const { getProjectRoot, getCurrentTimestamp } = require('./helpers');
log4js.configure({
appenders: {
console: { type: 'console' },
file: {
type: 'file',
filename: `${getProjectRoot()}/logs/${getCurrentTimestamp()}.log`
}
},
categories: {
default: { appenders: ['console', 'file'], level: 'debug' }
}
});
const logger = log4js.getLogger('mqtt-switchbot');
// Log4js Log Levels
// OFF
// FATAL
// ERROR
// WARN
// INFO
// DEBUG
// TRACE
// ALL
// The levels are cumulative.
// If you for example set the logging level to WARN all warnings, errors and fatals are logged
module.exports = {
trace,
info,
debug,
warn,
error,
line
};
function trace(...args) {
run(logger.trace, parseArgs(args));
}
function info(...args) {
run(logger.info, parseArgs(args));
}
function debug(...args) {
run(logger.debug, parseArgs(args));
}
function warn(...args) {
run(logger.warn, parseArgs(args));
}
function error(...args) {
run(logger.error, parseArgs(args));
}
function parseArgs(args) {
let sentence = args.shift();
if (typeof sentence === 'object') sentence = JSON.stringify(sentence);
return [sentence].concat(args);
}
function line() {
logger.debug('-------------------------------------------------------------------------------');
}
function run(f, args) {
f.apply(logger, args);
}