-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
258 lines (207 loc) · 6.77 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
const bunyan = require('bunyan');
const uuid = require('uuid');
const util = require('util');
const onFinished = require('on-finished');
const updateFields = (ctx, func, data, err) => {
if (!func) return data;
try {
if (err) {
return func.call(ctx, data, err) || data;
}
return func.call(ctx, data) || data;
} catch (e) {
ctx.log.error(e);
return data;
}
};
/*
* If logger is a bunyan logger instance, return it;
* otherwise, create a new logger with some reasonable defaults.
*/
const createOrUseLogger = (logger) => {
if (!logger || !logger.info || !logger.child) {
const loggerOpts = logger || {};
loggerOpts.name = loggerOpts.name || 'koa';
loggerOpts.serializers = loggerOpts.serializers || bunyan.stdSerializers;
logger = bunyan.createLogger(loggerOpts);
}
return logger;
};
/*
* Koa middleware that adds this.log property to the koa context
* containing a bunyan logger instance.
*
* Parameters:
* - loggerInstance: bunyan logger instance, or an object with properties
* that will be passed to bunyan.createLogger. If not
* specified, a default logger will be used.
*/
module.exports = (loggerInstance) => {
loggerInstance = createOrUseLogger(loggerInstance);
return (ctx, next) => {
ctx.log = loggerInstance;
return next();
};
};
/*
* Koa middleware that gets a unique request id from a header or
* generates a new one, and adds the requestId to all messages logged
* using this.log in downstream middleware and handlers.
*
* Must use(koaBunyanLogger()) before using this middleware.
*
* Parameters:
* - opts: object with optional properties:
* - header: name of header to get request id from (default X-Request-Id)
* - prop: property to store on 'this' context (default 'reqId')
* - requestProp: property to store on 'this.request' (default 'reqId')
* - field: log field name for bunyan (default 'req_id')
*/
module.exports.requestIdContext = (opts) => {
opts = opts || {};
const header = opts.header || 'X-Request-Id';
const ctxProp = opts.prop || 'reqId';
const requestProp = opts.requestProp || 'reqId';
const logField = opts.field || 'req_id';
return (ctx, next) => {
const reqId = ctx.request.get(header) || uuid.v4();
ctx[ctxProp] = reqId;
ctx.request[requestProp] = reqId;
const logFields = {};
logFields[logField] = reqId;
if (!ctx.log) {
throw new Error('must use(koaBunyanLogger()) before this middleware');
}
ctx.log = ctx.log.child(logFields);
return next();
};
};
/*
* Logs requests and responses.
*
* Must use(koaBunyanLogger()) before using this middleware.
*
* Parameters:
* - opts: object with optional properties
* - durationField: name of duration field
* - levelFn: function (status, err)
* - updateLogFields: function (data)
* - updateRequestLogFields: function (requestData)
* - updateResponseLogFields: function (responseData)
* - formatRequestMessage: function (requestData)
* - formatResponseMessage: function (responseData)
*/
module.exports.requestLogger = (opts) => {
opts = opts || {};
const levelFn = opts.levelFn || function (status) {
if (status >= 500) {
return 'error';
} if (status >= 400) {
return 'warn';
}
return 'info';
};
const durationField = opts.durationField || 'duration';
const formatRequestMessage = opts.formatRequestMessage || function () {
return util.format(' <-- %s %s',
this.request.method, this.request.originalUrl);
};
const formatResponseMessage = opts.formatResponseMessage || function (data) {
return util.format(' --> %s %s %d %sms',
this.request.method, this.request.originalUrl,
this.status, data[durationField]);
};
return (ctx, next) => {
if (Array.isArray(opts.ignorePath) && opts.ignorePath.includes(ctx.path)) {
return next();
}
let requestData = {
req: ctx.req
};
requestData = updateFields(ctx, opts.updateLogFields, requestData);
requestData = updateFields(ctx, opts.updateRequestLogFields, requestData);
ctx.log.info(requestData, formatRequestMessage.call(ctx, requestData));
const startTime = new Date().getTime();
let err;
const onResponseFinished = () => {
let responseData = {
req: ctx.req,
res: ctx.res
};
if (err) {
responseData.err = err;
}
responseData[durationField] = Date.now() - startTime;
responseData = updateFields(ctx, opts.updateLogFields, responseData);
responseData = updateFields(ctx, opts.updateResponseLogFields,
responseData, err);
const level = levelFn.call(ctx, ctx.status, err);
ctx.log[level](responseData,
formatResponseMessage.call(ctx, responseData));
// Remove log object to mitigate accidental leaks
ctx.log = null;
};
return next().catch((e) => {
err = e;
}).then(() => { // Emulate a finally
// Handle response logging and cleanup when request is finished
// This ensures that the default error handler is done
onFinished(ctx.response.res, onResponseFinished.bind(ctx));
if (err) {
throw err; // rethrow
}
});
};
};
/**
* Middleware which adds methods this.time(label) and this.timeEnd(label)
* to koa context.
*
* Parameters:
* - opts: object with the following optional properties
* - logLevel: name of log level to use; defaults to 'trace'
* - updateLogFields: function which will be called with
* arguments (fields) in koa context; can update fields or
* return a new object.
*
* Must use(koaBunyanLogger()) before using this middleware.
*/
module.exports.timeContext = (opts) => {
opts = opts || {};
const logLevel = opts.logLevel || 'trace';
const { updateLogFields } = opts;
function time(label) {
/* jshint validthis:true */
const startTimes = this._timeContextStartTimes;
if (startTimes[label]) {
this.log.warn('time() called for previously used label %s', label);
}
startTimes[label] = new Date().getTime();
}
function timeEnd(label) {
/* jshint validthis:true */
const startTimes = this._timeContextStartTimes;
const startTime = startTimes[label];
if (!startTime) { // whoops!
this.log.warn('timeEnd() called without time() for label %s', label);
return;
}
const duration = new Date().getTime() - startTime;
let fields = {
label,
duration,
msg: `${label}: ${duration}ms`
};
fields = updateFields(this, updateLogFields, fields);
this.log[logLevel](fields);
startTimes[label] = null;
}
return (ctx, next) => {
ctx._timeContextStartTimes = {};
ctx.time = time;
ctx.timeEnd = timeEnd;
return next();
};
};
// Export our copy of bunyan
module.exports.bunyan = bunyan;