-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogging.ts
More file actions
31 lines (27 loc) · 1.02 KB
/
logging.ts
File metadata and controls
31 lines (27 loc) · 1.02 KB
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
import express from 'express';
import { logger } from '../utils/logging';
export const logging = (
req: express.Request,
res: express.Response,
next: express.NextFunction,
): void => {
const startTime = process.hrtime();
res.on('finish', () => {
const [seconds, nanoseconds] = process.hrtime(startTime);
const responseTimeMs = seconds * 1000 + nanoseconds / 1e6;
logger.info({
status: res.statusCode,
method: req.method,
path: req.path,
didRenderEpic: res.locals.didRenderEpic,
didRenderBanner: res.locals.didRenderBanner,
bannerTargeting: res.locals.bannerTargeting,
epicTargeting: res.locals.epicTargeting,
userAgent: isServerError(res.statusCode) ? req.headers['user-agent'] : undefined,
epicSuperMode: res.locals.epicSuperMode,
responseTimeMs: Math.round(responseTimeMs),
});
});
next();
};
const isServerError = (statusCode: number) => statusCode >= 500;