Skip to content

Commit 3f1c77f

Browse files
committed
feat(log): do not log 'pre' request log unless configured to do so
1 parent 8b679ba commit 3f1c77f

File tree

6 files changed

+387
-385
lines changed

6 files changed

+387
-385
lines changed

config/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"$schema": "tsschema://../src/config/schema#ConfigurationSchema",
33
"logging": {
44
"level": "info",
5-
"logRequestBody": "env_switch:LOG_HTTP_REQUESTS"
5+
"logRequestBody": "env_switch:LOG_HTTP_REQUESTS",
6+
"preLog": "env_switch:HTTP_PRE_LOG"
67
},
78
"routing": {
89
"openapi": true,

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,31 +91,31 @@
9191
"lodash": "^4.17.21",
9292
"minimist": "^1.2.8",
9393
"opentelemetry-instrumentation-fetch-node": "^1.1.0",
94-
"pino": "^8.16.0",
94+
"pino": "^8.16.1",
9595
"read-pkg-up": "^7.0.1"
9696
},
9797
"devDependencies": {
98-
"@commitlint/cli": "^18.0.0",
99-
"@commitlint/config-conventional": "^18.0.0",
98+
"@commitlint/cli": "^18.2.0",
99+
"@commitlint/config-conventional": "^18.1.0",
100100
"@openapi-typescript-infra/coconfig": "^4.2.2",
101101
"@semantic-release/changelog": "^6.0.3",
102-
"@semantic-release/commit-analyzer": "^11.0.0",
102+
"@semantic-release/commit-analyzer": "^11.1.0",
103103
"@semantic-release/exec": "^6.0.3",
104104
"@semantic-release/git": "^10.0.1",
105-
"@semantic-release/release-notes-generator": "^12.0.0",
105+
"@semantic-release/release-notes-generator": "^12.1.0",
106106
"@types/cookie-parser": "^1.4.5",
107107
"@types/express": "^4.17.20",
108108
"@types/glob": "^8.1.0",
109109
"@types/lodash": "^4.14.200",
110110
"@types/minimist": "^1.2.4",
111-
"@types/node": "^20.8.7",
111+
"@types/node": "^20.8.10",
112112
"@types/supertest": "^2.0.15",
113-
"@typescript-eslint/eslint-plugin": "^6.8.0",
114-
"@typescript-eslint/parser": "^6.8.0",
113+
"@typescript-eslint/eslint-plugin": "^6.10.0",
114+
"@typescript-eslint/parser": "^6.10.0",
115115
"coconfig": "^1.0.0",
116-
"eslint": "^8.52.0",
116+
"eslint": "^8.53.0",
117117
"eslint-config-prettier": "^9.0.0",
118-
"eslint-plugin-import": "^2.28.1",
118+
"eslint-plugin-import": "^2.29.0",
119119
"pino-pretty": "^10.2.3",
120120
"pinst": "^3.0.0",
121121
"supertest": "^6.3.3",

src/config/schema.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export interface ConfigurationSchema extends BaseConfitSchema {
1111
level?: Level;
1212
logRequestBody?: boolean;
1313
logResponseBody?: boolean;
14+
// Whether to log a "pre" message when request processing starts. Most useful in
15+
// situations where there is some problem causing requests to hang
16+
preLog?: boolean;
1417
};
1518
routing?: {
1619
openapi?: boolean;

src/express-app/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export async function startApp<
124124
description: 'Duration of HTTP requests in seconds',
125125
});
126126

127-
app.use(loggerMiddleware(app, histogram, logging?.logRequestBody, logging?.logResponseBody));
127+
app.use(loggerMiddleware(app, histogram, logging));
128128

129129
// Allow the service to add locals, etc. We put this before the body parsers
130130
// so that the req can decide whether to save the raw request body or not.

src/telemetry/requestLogger.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,20 @@ export function loggerMiddleware<
108108
>(
109109
app: ServiceExpress<SLocals>,
110110
histogram: Histogram,
111-
logRequests?: boolean,
112-
logResponses?: boolean,
111+
config?: ConfigurationSchema['logging'],
113112
): RequestHandler {
114113
const { logger, service } = app.locals;
115114
return function gblogger(req, res, next) {
116115
const prefs: LogPrefs = {
117116
start: process.hrtime(),
118-
logRequests,
119-
chunks: logResponses ? [] : undefined,
117+
logRequests: config?.logRequestBody,
118+
chunks: config?.logResponseBody ? [] : undefined,
120119
logged: false,
121120
};
122121

123122
(res.locals as WithLogPrefs)[LOG_PREFS] = prefs;
124123

125-
if (logResponses) {
124+
if (config?.logResponseBody) {
126125
// res is a read-only stream, so the only way to intercept response
127126
// data is to monkey-patch.
128127
const oldWrite = res.write;
@@ -141,14 +140,16 @@ export function loggerMiddleware<
141140
}) as (typeof res)['end'];
142141
}
143142

144-
const preLog: Record<string, string | string[] | number | undefined> = {
145-
...getBasicInfo(req),
146-
ref: req.headers.referer || undefined,
147-
sid: (req as WithIdentifiedSession).session?.id,
148-
c: req.headers.correlationid || undefined,
149-
};
150-
service.getLogFields?.(req as RequestWithApp<SLocals>, preLog);
151-
logger.info(preLog, 'pre');
143+
if (config?.preLog) {
144+
const preLog: Record<string, string | string[] | number | undefined> = {
145+
...getBasicInfo(req),
146+
ref: req.headers.referer || undefined,
147+
sid: (req as WithIdentifiedSession).session?.id,
148+
c: req.headers.correlationid || undefined,
149+
};
150+
service.getLogFields?.(req as RequestWithApp<SLocals>, preLog);
151+
logger.info(preLog, 'pre');
152+
}
152153

153154
const logWriter = () => finishLog(app, undefined, req, res, histogram);
154155
res.on('finish', logWriter);

0 commit comments

Comments
 (0)