This repository has been archived by the owner on Feb 19, 2022. It is now read-only.
/
index.js
420 lines (369 loc) · 10.8 KB
/
index.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*!
* express-winston-middleware
* -------
* Winston log wrappers for Express.
*/
/**
* Logging wrapper.
*
* Binds configurations to actual Winston loggers and adds some common
* helpers.
*/
var os = require("os"),
cluster = require("cluster"),
url = require("url"),
_ = require("lodash"),
winston = require("winston"),
hostName = os.hostname(),
workerId,
serverId,
utils,
middleware,
Log;
// ----------------------------------------------------------------------------
// Helpers.
// ----------------------------------------------------------------------------
utils = {
clientIp: function (req) {
var forwards = req.header("x-forwarded-for"),
ipAddr = req.connection.remoteAddress,
firstIp,
ips;
if (forwards) {
ips = forwards.split(",");
firstIp = (ips[0] || "").replace(/^\s+|\s+$/, "");
if (firstIp) {
return firstIp;
}
}
return ipAddr;
}
};
// ----------------------------------------------------------------------------
// Middleware.
// ----------------------------------------------------------------------------
middleware = {
/**
* `request(opts, baseMeta)` - Express request middleware
*
* Creates a middleware function using base metadata. Integration:
*
* ```js
* app.use(winMid.request({
* transports: [ new (winston.transports.Console)({ json: true }) ]
* }, { foo: "bar" }));
* ```
*
* Once integrated, a logger will be attached to the response locals,
* and available as `res.locals._log`. The logger will then be removed at
* the end of the request.
*
* @param {Object} opts Winston logger options.
* @param {Object} baseMeta Metadata for all log statements.
* @api public
*/
request: function (opts, baseMeta) {
// Stash singleton logger for all requests.
var singleton = new winston.Logger(opts);
return function (req, res, next) {
// Create logger and attach to locals.
res.locals._log = new Log(_.extend({
singleton: singleton
}, opts), baseMeta);
var _end = res.end;
// Add request.
res.locals._log.addReq(req);
// Proxy end (what connect.logger does) to get status code.
res.end = function (chunk, encoding) {
var status = res.statusCode,
level;
// Unwind and call underlying end.
res.end = _end;
res.end(chunk, encoding);
// Allow controllers to wipe out logger.
if (!res.locals._log) { return; }
// Choose a warning and error level.
var levels = res.locals._log.levels;
// Find "lowest" and "highest" numbered levels.
// Frustratingly, for `cli`, `npm` low = info, high = error, but
// `syslog` is **reversed**. :(
var orderedLevels = _(levels)
.pairs()
.sortBy(function (p) { return p[1]; })
.map(function (p) { return p[0]; })
.value();
var lowLevel = _.first(orderedLevels);
var highLevel = _.last(orderedLevels);
// These _might_ not exist.
// Choose in order of preference and be permissive.
/*jshint sub:true*/
if (400 <= status && status < 500) {
// A "Warning".
level = level || (levels["warning"] && "warning");
level = level || (levels["warn"] && "warn");
level = level || highLevel;
} else if (500 <= status) {
// An "Error"
level = level || (levels["error"] && "error");
level = level || (levels["crit"] && "crit");
level = level || highLevel;
} else {
// "Info"
level = level || (levels["info"] && "info");
level = level || lowLevel;
}
/*jshint sub:false*/
// Add response info and log out.
res.locals._log.addRes(res);
res.locals._log[level]("request");
// Remove local logger.
delete res.locals._log;
};
return next();
};
},
/**
* `error(opts, baseMeta)` - Express error middleware
*
* Creates a middleware function for Express. Integration:
*
* ```
* app.use(winMid.error({
* transports: [ new (winston.transports.Console)({ json: true }) ]
* }, { foo: "bar" }));
* ```
*
* @param {Object} opts Winston logger options.
* @param {Object} baseMeta Metadata for log statements.
* @api public
*/
error: function (opts, baseMeta) {
// Stash singleton logger for all requests.
var singleton = new winston.Logger(opts);
var meta = _.extend({ type: "unhandled_error" }, baseMeta);
return function (err, req, res, next) {
// Create logger and add objects.
(new Log(_.extend({ singleton: singleton }, opts), meta))
.addReq(req)
.addRes(res)
.addError(err)
.error("unhandled error");
// Pass to underlying Express handler.
next(err);
};
},
/**
* `uncaught(opts, baseMeta)` - Global uncaught exception handler
*
* Creates a handler function for any uncaught exception. Integration:
*
* ```
* process.on("uncaughtException", winMid.uncaught({
* transports: [ new (winston.transports.Console)({ json: true }) ]
* }, { foo: "bar" }));
* ```
*
* **Note**: Terminates process at end.
*
* @param {Object} opts Winston logger options.
* @param {Object} baseMeta Metadata for log statements.
* @api public
*/
uncaught: function (opts, baseMeta) {
// Stash singleton logger for all requests.
var singleton = new winston.Logger(opts);
var meta = _.extend({ type: "unhandled_error" }, baseMeta);
return function (err) {
try {
// Try real logger.
return (new Log(_.extend({ singleton: singleton }, opts), meta))
.addError(err)
.error("Uncaught exception");
} catch (other) {
// Else, give up and use straight console logging.
console.log((err || {}).stack || err || "Unknown");
console.log("Error: Hit additional error logging the previous error.");
console.log((other || {}).stack || other || "Unknown");
return;
} finally {
process.exit(1);
}
};
}
};
// ----------------------------------------------------------------------------
// Stashed variables.
// ----------------------------------------------------------------------------
/*!
* Infer worker id or master.
*/
workerId = process.env.NODE_WORKER_ID || (cluster.worker || {}).id || null;
serverId = workerId ? "w" + workerId : "m";
// ----------------------------------------------------------------------------
// Classes.
// ----------------------------------------------------------------------------
/**
* `Log(opts, baseMeta)` - Logger class.
*
* Wraps Winston logger with additional functionality.
*
* ```
* var log = new winMid.Log({
* transports: [ new (winston.transports.Console)({ json: true }) ]
* }, { foo: "bar" }));
* ```
*
* @param {Object} opts Winston logger options.
* @param {Object} opts.singleton Optional singleton Winston logger to use.
* @param {Object} baseMeta Metadata for all log statements.
* @api public
*/
Log = function (opts, baseMeta) {
// Stash self and any singleton designated.
var self = this;
var singleton = (opts || {}).singleton;
// Update options.
opts = _.omit(opts, "singleton");
// Create internal, real Winston logger.
this._log = singleton || new winston.Logger(opts);
// Expose levels.
this.levels = this._log.levels;
// Meta for all log statements.
this._meta = _.merge({
env: process.env.NODE_ENV || "development",
server: {
id: serverId,
pid: process.pid,
hostName: hostName
}
}, baseMeta);
// Passthrough transform function.
this._metaTransformFn = null;
// Iterate and patch all log levels.
_.each(this.levels, function (num, level) {
self[level] = function (msg, metaOrCb, callback) {
var meta = _.extend({ date: (new Date()).toISOString(), }, this._meta),
args;
// Extend with user-passed meta, if applicable.
if (_.isObject(metaOrCb)) {
_.extend(meta, metaOrCb, {});
}
// Apply final transform, if any.
if (self._metaTransformFn) {
meta = self._metaTransformFn(_.cloneDeep(meta));
}
// Infer arguments per Winston calling conventions.
if (arguments.length === 2 && _.isFunction(metaOrCb)) {
// Push callback to end.
args = [msg, meta, metaOrCb];
} else if (arguments.length > 2) {
// In order already.
args = [msg, meta, callback];
} else {
// No callback.
args = [msg, meta];
}
// Call real logger.
return this._log[level].apply(this._log, args);
};
});
};
/**
* `Log.addMeta(meta)`
*
* Add arbitrary meta to all subsequent log statements.
*
* @param {Object} meta Metadata object.
* @api public
*/
Log.prototype.addMeta = function (meta) {
_.merge(this._meta, meta);
return this;
};
/**
* `Log.addReq(req)`
*
* Add request to meta.
*
* @param {Object} req Request object.
* @api public
*/
Log.prototype.addReq = function (req) {
var maxChars = 200,
urlObj = url.parse(req.url);
this.addMeta({
req: {
method: req.method,
host: req.headers.host,
path: (urlObj.pathname || "").substr(0, maxChars),
query: (urlObj.query || "").substr(0, maxChars),
client: {
ip: utils.clientIp(req)
}
}
});
return this;
};
/**
* `Log.transformMeta(fn)`
*
* Set a delayed single transform function to mutate a **copy** of the metadata
* _right before_ a logging event. You can only presently have **one** such
* function. And it is delayed so that for things like request end, you can
* effectively access **all** the metadata.
*
* The transform is applied on each log call and passes a copy of the mutated
* metadata to the actual log call.
*
* The function signature should be `fn(existingMeta)` and return mutated
* metadata.
*
* @param {Function} fn Transform function.
* @api public
*/
Log.prototype.transformMeta = function (fn) {
this._metaTransformFn = fn;
return this;
};
/**
* `Log.addRes(res)`
*
* Add response to meta.
*
* @param {Object} res Response object.
* @api public
*/
Log.prototype.addRes = function (res) {
this.addMeta({
res: {
statusCode: res.statusCode
}
});
return this;
};
/**
* `Log.addError(err)`
*
* Add error to meta.
*
* @param {Error} err Error object.
* @api public
*/
Log.prototype.addError = function (err) {
var maxChars = 200;
this.addMeta({
err: {
msg: _.isNull(err.message) ? err.toString() : err.message,
args: (err.arguments || "").toString().substr(0, maxChars),
type: err.type || null,
stack: (err.stack || "").split("\n"),
}
});
return this;
};
module.exports = {
Log: Log,
request: middleware.request,
uncaught: middleware.uncaught,
error: middleware.error
};