-
Notifications
You must be signed in to change notification settings - Fork 3
/
logging.js
62 lines (53 loc) · 1.41 KB
/
logging.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
let config = require('config').get('logging');
const levels = {
none: 0,
error: 1,
warn: 2,
info: 3,
debug: 4
};
exports.setLevel = function (level) {
config = level;
}
function willLog(level) {
return !config || levels[level] <= levels[config];
}
exports.willLog = willLog;
exports.debug = function (...params) {
if (willLog('debug')) {
writeLog('debug', params);
}
}
exports.info = function (...params) {
if (willLog('info')) {
writeLog('info', params);
}
}
exports.warn = function (...params) {
if (willLog('warn')) {
writeLog('warn', params);
}
}
exports.error = function (...params) {
if (willLog('error')) {
if (params && params.length > 0 && params[0] && params[0].stack && params[0].message) {
let e = params[0];
params = params.slice(1);
params.push(e.message, e.stack);
}
writeLog('error', params);
}
}
function writeLog(level, params) {
if (level === 'error') {
console.error(formatDate(), level.toUpperCase(), ...params);
} else {
console.log(formatDate(), level.toUpperCase(), ...params);
}
}
// make time local instead of UTC and remove T and Z from ISO format
function formatDate() {
let now = new Date();
now = new Date(now.getTime() - (now.getTimezoneOffset()*60*1000));
return now.toISOString().replace('T', ' ').substring(0, 23);
}