feat(logging): replace Winston with Pino structured JSON logging and correlation IDs - #2
Open
CodedSceptre wants to merge 1 commit into
Open
feat(logging): replace Winston with Pino structured JSON logging and correlation IDs#2CodedSceptre wants to merge 1 commit into
CodedSceptre wants to merge 1 commit into
Conversation
…on IDs - Rewrite logger.ts: swap Winston for Pino v9 (JSON output, Loki-compatible) - Log level driven by LOG_LEVEL env var (default: info) - Sensitive field redaction at serialisation time: authorization, password, token, accessToken, refreshToken, privateKey, apiKey, secret, mnemonic, seed - correlationIdMiddleware(): reads X-Request-ID or X-Correlation-ID header, falls back to UUID v4; echoes ID in X-Correlation-ID response header - createRequestLogger(): logs method, path, status, durationMs, correlationId for every request; skips /api/health to reduce noise; uses error/warn/info level based on HTTP status - Child logger (req.log) attached to every request for downstream use - Update LOG_LEVEL enum in config/index.ts to Pino levels: fatal | error | warn | info | debug | trace - Add pino@^9.7.0 and pino-http@^10.4.0 to package.json - 14 Vitest tests: logger methods, correlationId middleware (UUID gen, header propagation, child logger), request logger (flow, health skip, level selection, correlation ID propagation), stream-injection log content Closes #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace Winston with Pino for structured JSON logging with per-request correlation ID tracing.
What was implemented
backend/src/logger.ts) — replaces Winston with a Pino instance configured from theLOG_LEVELenv var (supportsfatal,error,warn,info,debug,trace)redactoption stripsauthorization,password,token,privateKey,apiKey,secret,mnemonic, and nested variants (body.password,req.headers.authorization, etc.) before writing any log linecorrelationIdMiddleware()) — readsX-Request-ID(priority) orX-Correlation-IDfrom incoming headers, falls back to a fresh UUID v4; stampsreq.correlationIdand a childreq.loglogger; echoes the ID back in theX-Correlation-IDresponse headercreateRequestLogger()) — logs one structured JSON line per request/response containingcorrelationId,method,path,status,durationMs,ip; skips/api/healthto reduce noise; useserror/warn/infolevel based on HTTP statuslevelas a string label (not an integer), matching Promtail's label-extraction pipelinebackend/src/config/index.ts) —LOG_LEVELZod enum updated from Winston's levels (http,verbose,silly) to Pino's levels (fatal,trace)backend/.env.example) — comment clarifies the Pino level namesTests
backend/src/middleware/__tests__/pinoLogger.test.tscovering:correlationIdMiddleware— UUID generation, header propagation (X-Request-IDandX-Correlation-ID),req.logchild logger attachmentcreateRequestLogger— response flow, health-check exclusion, status-level mappingAcceptance criteria
LOG_LEVELenv varX-Request-IDheader or generated if absentreq.logchild loggerCloses soterika#298