Files
talk/src/core/server/app/middleware/logging.ts
T
Wyatt Johnson 02e1236792 initial commit
2018-06-16 17:20:51 -06:00

44 lines
1.1 KiB
TypeScript

import { RequestHandler, ErrorRequestHandler } from 'express';
import logger from '../../logger';
import now from 'performance-now';
export const access: RequestHandler = (req, res, next) => {
const startTime = now();
const end = res.end;
res.end = (chunk: any, encodingOrCb?: string | Function, cb?: Function) => {
// Compute the end time.
const responseTime = Math.round(now() - startTime);
// Get some extra goodies from the request.
const userAgent = req.get('User-Agent');
// Reattach the old end, and finish.
res.end = end;
if (typeof encodingOrCb === 'function') {
res.end(chunk, encodingOrCb);
} else {
res.end(chunk, encodingOrCb, cb);
}
// Log this out.
logger.info(
{
// traceID: req.id,
url: req.originalUrl || req.url,
method: req.method,
statusCode: res.statusCode,
userAgent,
responseTime,
},
'http request'
);
};
next();
};
export const error: ErrorRequestHandler = (err, req, res, next) => {
logger.error({ err }, 'http error');
next(err);
};