-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssi-logger.js
227 lines (195 loc) · 7.46 KB
/
ssi-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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"use strict";
const _ = require('lodash');
const fs = require('fs');
const os = require('os');
const util = require('util');
const logformat = require('logformat');
const filterObject = require('./lib/filterObject.js');
const path = require('path');
const uuid = require('uuid');
// System wide configuration files in search/override order.
const conf_files = [
'./ssi-logger.conf.defaults',
'/etc/ssi-logger.conf',
'/etc/ssi-logger.conf.local',
'/usr/local/etc/ssi-logger.conf',
'/usr/local/etc/ssi-logger.conf.local',
];
function loadConf(files) {
module.exports.options = {
messageMaxLength: 8192,
censor: [],
transports: {
amqp: {
url: 'amqp://guest:guest@localhost/',
enable: false,
},
console: {
enable: process.env.NODE_ENV !== 'production'
},
syslog: {
enable: process.env.NODE_ENV !== 'production'
},
},
};
_.forEach(files, (filepath) => {
filepath = path.resolve(__dirname, filepath.split("/").join(path.sep));
try {
const conf = JSON.parse(fs.readFileSync(filepath).toString());
_.merge(module.exports.options, conf);
} catch (e) {
// Ignore file not found error, but report others
// like configuration file syntax errors.
if (e.code === 'ENOENT') {
return;
}
console.error({file: filepath, err: e});
console.error(e.stack);
process.exit(1);
}
});
}
function log(level, message) {
// Censor objects.
const args = _.map(arguments.length > 1 ? _.tail(arguments) : [], function (arg) {
return filterObject(arg, module.exports.censor());
});
// Map level synonyms.
switch (level) {
case 'ERR': level = 'ERROR'; break;
case 'WARNING': level = 'WARN'; break;
}
message = util.format.apply(null, _.map(args, logformat)).substring(0, module.exports.options.messageMaxLength);
// Censor any key=value pairs appearing in the formatted message.
module.exports.censor().forEach(function (key) {
var safeKey; // string that can be safely inserted into a regex.
// when the key is a regexp and it contains a group, we need to offset where we find the value
var offset = 0;
if (key instanceof RegExp) { // if key is already a RegExp, extract the source pattern.
safeKey = key.source;
offset = (key.source.match(/\(/g) || []).length;
} else { // else it's a string, escape regex operators that may be present.
safeKey = key.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
}
// create a regex that matches key=val and key="val"
var re = new RegExp("(" + safeKey + ")" + "=([^\"][^\\s]+|\"[^\"]*\")", "g");
//replace each character of the value with an 'X' to censor it.
message = message.replace(re, function (match, key) {
return key + '=[redacted]';
});
});
// Note that os.hostname() appears to only return FQDN when
// the machine has a proper DNS A record.
process.emit('log', {
version: '1.1.0',
created: new Date(),
host: os.hostname(),
level: level,
message: message,
data: args,
eid: uuid.v4().substring(0, 8),
});
return message;
}
function censor(list) {
module.exports.censorList = module.exports.censorList || [];
if (Array.isArray(list)) {
module.exports.censorList = _.uniq(list);
}
return module.exports.censorList;
}
function defaults() {
var defaultMessages = Array.prototype.slice.call(arguments);
var defaultLog = function (level, message) {
return module.exports.apply(null, _.union(Array.prototype.slice.call(arguments), defaultMessages));
};
_.extend(defaultLog, module.exports);
defaultLog.defaults = function () {
return defaults.apply(null, _.union(defaultMessages, Array.prototype.slice.call(arguments)));
};
addConvenienceFunctions(defaultLog);
return defaultLog;
}
const activeTransports = {};
function dispatcher(event) {
Object.keys(activeTransports)
.filter((transport) => activeTransports[transport].filter(event))
.forEach((transport) => activeTransports[transport].chunkify(event).forEach((chunk) => activeTransports[transport].log(chunk)));
}
function close(optDone) {
process.removeListener('log', dispatcher);
let closed = 0;
const transports = Object.keys(activeTransports);
// ensure optDone is called when there are no transports
if (transports.length === 0 && typeof optDone === 'function') {
return optDone();
}
transports.forEach((transport) => {
activeTransports[transport].end(() => {
if (transports.length <= ++closed) {
if (optDone) {
optDone();
}
}
});
delete activeTransports[transport];
});
}
// Install a transport if options contains a property with a name
// that matches a known transport and has `enable` set to `true`.
function open(options, user_transports) {
close();
options = _.defaultsDeep(options, module.exports.options.transports);
const mergedTransports = _.merge({}, transports, user_transports);
_.forEach(options, (args, transport) => {
if (_.isObject(args) && _.get(args, 'enable', true) === true && _.has(mergedTransports, transport)) {
activeTransports[transport] = new mergedTransports[transport](args);
}
});
process.on('log', dispatcher);
}
function transformLogEvent(log_event) {
return [
function legacyToVersion1_0_0(log_event) {
// Does it look like a legacy event?
if (_.isObject(log_event) && _.has(log_event, 'level') && _.has(log_event, 'message') && !_.has(log_event, 'version')) {
_.defaultsDeep(log_event, {
version: '1.0.0',
host: os.hostname(),
created: new Date(),
data: [],
});
}
return log_event;
},
].reduce((result, transform) => transform(result), log_event);
}
// Public API
module.exports = log;
module.exports.censor = censor;
module.exports.close = close;
module.exports.defaults = defaults;
module.exports.loadConf = loadConf; // For testing.
module.exports.open = open;
module.exports.transformLogEvent = transformLogEvent;
module.exports.levelNames = require('./lib/logLevelNames');
module.exports.activeTransports = activeTransports;
module.exports.Transport = require('./lib/Transport'); // Expose for user transports.
function addConvenienceFunctions(logger) {
// Emulate the logger.level() API of winston so we can use our logger implementation as a drop in replacement
logger.log = function () { logger.apply(null, Array.prototype.slice.call(arguments)); };
_.forEach(log.levelNames, function (level) {
logger[level.toLowerCase()] = function () {
return logger.apply(null, _.union([level], Array.prototype.slice.call(arguments)));
};
});
}
loadConf(conf_files);
censor(module.exports.options.censor);
addConvenienceFunctions(module.exports);
const transports = {
amqp: require('./lib/transports/amqp'),
console: require('./lib/transports/console'),
stream: require('./lib/transports/stream'),
syslog: require('./lib/transports/syslog'),
};