Add koaBunyanLogger.timeContext(opts) (#41977)

https://github.com/koajs/bunyan-logger#koabunyanloggertimecontextopts

> koaBunyanLogger.timeContext(opts)
> Adds time(label) and timeEnd(label) methods to the koa context, which records the time between the time() and timeEnd() calls for a given label.
>
> Calls to time() and timeEnd() can be nested or interleaved as long as they're balanced for each label.
>
> Options:
>
> 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.
This commit is contained in:
Alex Robertson
2020-01-30 13:15:39 -08:00
committed by GitHub
parent c388d9eb1e
commit e3f24692a8
2 changed files with 20 additions and 0 deletions
+6
View File
@@ -39,8 +39,14 @@ declare namespace koaBunyanLogger {
ignorePath?: string[];
}
interface TimeContextOptions {
logLevel?: string;
updateLogFields?: (fields: any) => any;
}
function requestLogger(opts?: RequestLoggerOptions): Middleware;
function requestIdContext(opts?: RequestIdContextOptions): Middleware;
function timeContext(opts?: TimeContextOptions): Middleware;
}
// Extend the Koa context to add the logger..
@@ -6,6 +6,20 @@ app.use(koaBunyanLogger());
app.use(koaBunyanLogger.requestIdContext());
app.use(koaBunyanLogger.requestLogger());
app.use(koaBunyanLogger.requestLogger({ ignorePath: ['/ping'] }));
app.use(koaBunyanLogger.timeContext());
app.use(
koaBunyanLogger.timeContext({
logLevel: 'warn',
updateLogFields(fields) {
return {
request_trace: {
name: fields.label,
time: fields.duration,
},
};
},
}),
);
app.use(async (ctx, next) => {
ctx.log.info('Got a request from %s for %s', ctx.request.ip, ctx.path);