Skip to content

Commit

Permalink
Make HTTP_LOG_PATH optional and disable HTTP logging in production (w…
Browse files Browse the repository at this point in the history
…e rarely will need this additional detailed logging)
  • Loading branch information
johnwarden authored and mergify[bot] committed Sep 11, 2024
1 parent d19822b commit 5950fbf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
1 change: 0 additions & 1 deletion Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion app/utils/env.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
44 changes: 23 additions & 21 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 5950fbf

Please sign in to comment.