Add configurable structured logging

Adds a leveled JSON logger (error/warn/info/verbose/debug, controlled via
LOG_LEVEL) wired into the Express API (access log, error middleware,
crash handlers, memory heartbeat) and the Next.js server (page-request
proxy, instrumentation crash handlers, heartbeat). LOG_LEVEL is exposed
through compose.yaml, and both services now rotate their Docker logs
(json-file, 20m x 10 files) instead of growing unbounded. Intended to
capture long-running diagnostic data for the intermittent 502s seen on
the Docker host.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Julian Appel 2026-08-06 19:45:39 +02:00
parent f26c000007
commit ea3c02cd6b
12 changed files with 317 additions and 6 deletions

View file

@ -0,0 +1,25 @@
import { createLogger, toErrorMeta } from "./shared/logging/logger";
export function registerNodeInstrumentation() {
const logger = createLogger("web");
const heartbeatIntervalMs = 5 * 60 * 1000;
logger.info("web server starting", { pid: process.pid });
process.on("uncaughtException", (error) => {
logger.error("uncaught exception", toErrorMeta(error));
});
process.on("unhandledRejection", (reason) => {
logger.error("unhandled rejection", toErrorMeta(reason));
});
setInterval(() => {
const memory = process.memoryUsage();
logger.verbose("heartbeat", {
uptimeSeconds: Math.round(process.uptime()),
rssMb: Math.round(memory.rss / 1024 / 1024),
heapUsedMb: Math.round(memory.heapUsed / 1024 / 1024),
});
}, heartbeatIntervalMs).unref();
}