forked from LiskArchive/lisk-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
144 lines (125 loc) · 2.86 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
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
140
141
142
143
144
/*
* Copyright © 2018 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
'use strict';
var fs = require('fs');
var util = require('util');
var child_process = require('child_process');
var path = require('path');
var strftime = require('strftime').utc();
require('colors');
module.exports = function(config) {
config = config || {};
var exports = {};
config.levels = config.levels || {
none: 99,
trace: 0,
debug: 1,
log: 2,
info: 3,
warn: 4,
error: 5,
fatal: 6,
};
config.level_abbr = config.level_abbr || {
trace: 'trc',
debug: 'dbg',
log: 'log',
info: 'inf',
warn: 'WRN',
error: 'ERR',
fatal: 'FTL',
};
config.filename = `${process.cwd()}/${config.filename || 'logs.log'}`;
config.errorLevel = config.errorLevel || 'log';
child_process.execSync(`mkdir -p ${path.dirname(config.filename)}`);
var log_file = fs.createWriteStream(config.filename, { flags: 'a' });
exports.setLevel = function(errorLevel) {
config.errorLevel = errorLevel;
};
function snipsecret(data) {
for (var key in data) {
if (key.search(/secret/i) > -1) {
data[key] = 'XXXXXXXXXX';
}
}
return data;
}
Object.keys(config.levels).forEach(name => {
function log(message, data) {
var log = {
level: name,
timestamp: strftime('%F %T', new Date()),
};
if (message instanceof Error) {
log.message = message.stack;
} else {
log.message = message;
}
if (data && util.isObject(data)) {
log.data = JSON.stringify(snipsecret(data));
} else {
log.data = data;
}
log.symbol = config.level_abbr[log.level]
? config.level_abbr[log.level]
: '???';
if (config.levels[config.errorLevel] <= config.levels[log.level]) {
if (log.data) {
log_file.write(
util.format(
'[%s] %s | %s - %s\n',
log.symbol,
log.timestamp,
log.message,
log.data
)
);
} else {
log_file.write(
util.format(
'[%s] %s | %s\n',
log.symbol,
log.timestamp,
log.message
)
);
}
}
if (
config.echo &&
config.levels[config.echo] <= config.levels[log.level]
) {
if (log.data) {
console.info(
`[${log.symbol.bgYellow.black}]`,
log.timestamp.grey,
'|',
log.message,
'-',
log.data
);
} else {
console.info(
`[${log.symbol.bgYellow.black}]`,
log.timestamp.grey,
'|',
log.message
);
}
}
}
exports[name] = log;
});
return exports;
};