From 5950fbf6d7fbd190ec8e67d7dbbb40a8828e14ef Mon Sep 17 00:00:00 2001 From: "Jonathan R. Warden" Date: Tue, 10 Sep 2024 15:21:44 -0600 Subject: [PATCH] Make HTTP_LOG_PATH optional and disable HTTP logging in production (we rarely will need this additional detailed logging) --- Earthfile | 1 - app/utils/env.server.ts | 2 +- server/index.ts | 44 +++++++++++++++++++++-------------------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Earthfile b/Earthfile index 947a65fe..98fc0248 100644 --- a/Earthfile +++ b/Earthfile @@ -121,7 +121,6 @@ docker-image: ENV APP_DATABASE_URL="file:$APP_DATABASE_PATH" ENV CACHE_DATABASE_FILENAME="cache.db" ENV CACHE_DATABASE_PATH="/data/$CACHE_DATABASE_FILENAME" - ENV HTTP_LOG_PATH="/data/http-log.jsonl" ENV INTERNAL_PORT="8080" ENV PORT="8081" diff --git a/app/utils/env.server.ts b/app/utils/env.server.ts index 81394c5f..02d30e94 100644 --- a/app/utils/env.server.ts +++ b/app/utils/env.server.ts @@ -5,7 +5,7 @@ const schema = z.object({ APP_DATABASE_PATH: z.string(), APP_DATABASE_URL: z.string(), GB_DATABASE_PATH: z.string(), - HTTP_LOG_PATH: z.string(), + HTTP_LOG_PATH: z.optional(z.string()), SESSION_SECRET: z.string(), INTERNAL_COMMAND_TOKEN: z.string(), HONEYPOT_SECRET: z.string(), diff --git a/server/index.ts b/server/index.ts index 34d5e474..de640885 100644 --- a/server/index.ts +++ b/server/index.ts @@ -96,27 +96,29 @@ app.use( app.use(express.static('public', { maxAge: '1h' })) const httpLogPath = process.env.HTTP_LOG_PATH -const logger = pino(pino.destination(httpLogPath)) - -// Use pino-http middleware with custom logging -app.use( - pinoHttp({ - logger, - serializers: { - // https://github.com/pinojs/pino-http/issues/5#issuecomment-955748053 - res: pino.stdSerializers.wrapResponseSerializer((res: any) => { - return { - statusCode: res.raw.statusCode, - // Allowlist useful headers - headers: { - 'content-type': res.raw.headers['content-type'], - 'content-length': res.raw.headers['content-length'], - }, - } - }), - }, - }), -) +if (httpLogPath !== undefined) { + const logger = pino(pino.destination(httpLogPath)) + + // Use pino-http middleware with custom logging + app.use( + pinoHttp({ + logger, + serializers: { + // https://github.com/pinojs/pino-http/issues/5#issuecomment-955748053 + res: pino.stdSerializers.wrapResponseSerializer((res: any) => { + return { + statusCode: res.raw.statusCode, + // Allowlist useful headers + headers: { + 'content-type': res.raw.headers['content-type'], + 'content-length': res.raw.headers['content-length'], + }, + } + }), + }, + }), + ) +} app.get(['/build/*', '/img/*', '/fonts/*', '/favicons/*'], (req, res) => { // if we made it past the express.static for these, then we're missing something.