-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmicrologger.js
294 lines (253 loc) · 9.17 KB
/
micrologger.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
'use strict';
const STRIP_ANSI = require('strip-ansi');
const UUID = require('uuid');
const REQ_PATH = require('app-root-path').require;
const OS = require('os');
const COLLECTORS = require('./collectors');
const LEVELS = require('./levels');
class Micrologger {
constructor (opts={}) {
this.name = opts.name || REQ_PATH('/package.json').name;
this.color = opts.color || false;
this.bold = opts.bold || false;
this.backgroundColor = opts.backgroundColor || false;
this.stripAnsi = (opts.stripAnsi === false ? false : true);
this.hostname = opts.hostname || OS.hostname();
this.collectors = opts.collectors || {};
this.requestIdHeader = opts.requestIdHeader || 'x-request-id';
this.correlationHeader = opts.correlationHeader || 'x-correlation-id';
this.correlationGen = opts.correlationGen || (() => UUID.v4());
this.levels = opts.levels || false;
this.levelSeverity = opts.level || opts.severity || 'ALL';
this.levelValue = Infinity;
this.formatReq = opts.formatReq || ((ctx) => {
return `<-- ${ctx.method} ${ctx.path}`;
});
this.formatRes = opts.formatRes || ((ctx) => {
return `--> ${ctx.method} ${ctx.path} ${ctx.status}`;
})
// If a single collector was specified, add it
let collectors = Object.keys(this.collectors);
if (opts.collector && !collectors.length) {
this.useCollector(opts.collector);
// Add the list of collectors passed
} else {
for (let collector of collectors) {
let cltr = this.collectors[collector];
this.useCollector(cltr, cltr);
}
}
// If custom levels were not set, apply default syslog
if (!this.levels) {
this.levels = {};
let levels = Object.keys(LEVELS);
for (let severity of levels) {
if (severity === 'level') continue;
this.levels[severity] = new LEVELS[severity]({
color : this.color,
bold : this.bold,
backgroundColor : this.backgroundColor,
});
}
// Validate custom levels
} else {
let levels = Object.keys(this.levels);
for (let severity of levels) {
if (!(this.levels[severity] instanceof LEVELS.level)) {
throw Error(`Attempted to use level which is not a subclass of Level: [${severity}]`);
}
}
}
// find the lowest level allowed to be logged (if passed as an option)
if (this.levelSeverity && (opts.level || opts.severity)) {
let level = this.levels[this.levelSeverity.toLowerCase()];
if (!level) {
throw Error(`Attempted to set unknown logging level [${opts.levelSeverity}]`);
}
this.levelValue = level.value;
}
// Enable/disable levels based on value, and expose them on the base of the object
this.setLevel(this.levelSeverity)
let levels = Object.keys(this.levels);
for (let level of levels) {
let lvl = this.levels[level];
if (this[lvl.severity] && !this[lvl.severity].isLevel) {
throw Error(`Attempted to overwrite existing logger propterty level with severity [${level}]`)
}
if (this[lvl.keyword] && !this[lvl.keyword].isLevel) {
throw Error(`Attempted to overwrite existing logger propterty level keyword [${level}]`)
}
this[lvl.severity] = this[lvl.keyword] = (data) => this.log(level, data);
this[lvl.severity].isLevel = this[lvl.keyword].isLevel = true
}
}
// Add the collector for the logger
useCollector (collector, config={}) {
if (!collector) {
throw Error('Attempted to set undefined collector');
}
let type = ((typeof collector === 'string' ? collector: collector.type) || '').toLowerCase();
// Custom collector injection
if (collector instanceof COLLECTORS.collector) {
this.collectors[type] = collector;
return;
}
// Known collector configuration
if (!type) {
throw Error('Collector type not specified in setCollector');
}
let Collector = COLLECTORS[type];
if (!Collector) {
throw Error(`Collector type [${type}] not known`);
}
this.collectors[type] = new Collector(config);
}
// set color for levels
setColor (color) {
let levels = Object.keys(LEVELS);
for (let severity of levels) {
if (severity === 'level') continue;
this.levels[severity].setColor(color);
}
}
// set boldness for levels
setBold (bold=true) {
let levels = Object.keys(LEVELS);
for (let severity of levels) {
if (severity === 'level') continue;
this.levels[severity].bold = bold;
}
}
// set whether or not to strip ansi encoding
setStripAnsi (bool=true) {
this.stripAnsi = bool;
}
// set what level to log to
setLevel (severity) {
this.levelSeverity = severity;
let levels = Object.keys(this.levels);
for (let level of levels) {
let lvl = this.levels[level];
let lvlVal = lvl.value
if (lvlVal <= this.levelValue) {
this.levels[level].enable();
} else {
this.levels[level].disable();
}
}
}
collect (data={}) {
let names = Object.keys(this.collectors || []);
for (let name of names) {
this.collectors[name].collect(data);
}
}
log (level='', data='') {
let norm = {};
// Assign string s object
if (typeof data === 'string') {
norm.message = data;
// Naive clone to leave data unaffected
} else {
let keys = Object.keys(data);
for (let key of keys) norm[key] = data[key];
}
// Perform normalizations
norm.severity = level.toUpperCase();
norm.level = level.toLowerCase();
norm.message = this.stripAnsi ? STRIP_ANSI(norm.message): norm.message;
// Use the appropriate logger
let logger = this.levels[norm.level];
if (!logger) {
return console.warn(`Attempted to use unknown log level [${norm.level}] for message: ${norm.message}`);
}
if (logger.enabled) {
logger.log(norm);
this.collect(norm);
}
}
// Middleware for req/res logging
middleware (opts={}) {
let correlationHeader = opts.correlationHeader || this.correlationHeader;
let formatReq = opts.formatReq || this.formatReq;
let formatRes = opts.formatRes || this.formatRes;
let correlationGen = opts.correlationGen || this.correlationGen;
let requestIdHeader = opts.requestIdHeader || this.requestIdHeader;
return async (ctx, next) => {
let res = ctx.res;
let reqTime = new Date();
let reqId = UUID.v4();
let classname = (ctx.request.headers[correlationHeader]) ? 'service_request' : 'client_request';
let correlationId = ctx.request.headers[correlationHeader] || correlationGen();
ctx.request.headers[correlationHeader] = correlationId;
ctx.request.headers[requestIdHeader] = correlationId;
let done = (evt) => {
let resTime = new Date();
res.removeListener('finish', onFinish);
res.removeListener('close', onClose);
let logBase = {
requestId : reqId,
classname : classname,
correlationId : correlationId,
resolutionTime : resTime - reqTime,
requestTime : reqTime.toString(),
responseTime : res.toString(),
formatRes : formatReq,
formatRes : formatRes,
};
logBase.message = formatReq(ctx);
this.log('informational', this.buildReqLog(ctx, logBase));
let resLevel = (ctx.response.status >= 400 ? 'informational' : 'error');
logBase.message = formatRes(ctx);
this.log(resLevel, this.buildResLog(ctx, logBase));
};
let onFinish = done.bind(null, 'finish');
let onClose = done.bind(null, 'close');
res.once('finish', onFinish);
res.once('close', onClose);
try {
await next();
} catch (err) {
throw err;
}
}
}
//
// Helpers
//
buildReqLog (ctx, base) {
return {
request_id : base.requestId,
class : base.classname,
correlation_id : base.correlationId,
request_time : base.requestTime,
message : base.message,
ident : this.name,
host : this.hostname,
client : ctx.request.ip || ctx.request.headers['x-forwarded-for'],
path : ctx.path,
method : ctx.method,
severity : 'informational',
metadata : {}
}
}
buildResLog (ctx, base) {
return {
request_id : base.requestId,
class : base.classname,
correlation_id : base.correlationId,
message : base.message,
resolution_time : base.resolvedTime,
response_time : base.resolutionTime.toString(),
ident : this.name,
host : this.hostname,
client : ctx.request.ip || ctx.request.headers['x-forwarded-for'],
path : ctx.path,
method : ctx.method,
status : ctx.response.status,
severity : ctx.response.status >= 400 ? 'error' : 'informational',
metadata : {}
}
}
}
module.exports = Micrologger;