-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
executable file
·85 lines (70 loc) · 1.7 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
// 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('fitbit-to-influx');
// 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 = {
logRequest,
trace,
info,
debug,
warn,
error,
line
};
function logRequest(request, entity) {
request = request.split('.');
// const api = request[0];
const model = request[1];
const method = request[2];
// logger.debug('%s.%s - received %s%s', api, model, method, (entity ? ` for ${entity}` : ''));
logger.debug(`received ${method.toUpperCase()} /${model} ${entity ? `for ${entity}` : ''}`);
}
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);
}