This PR implements request-scoped logging context (req.log) on Express request objects. Route handlers and middlewares can now call req.log.info(), req.log.warn(), req.log.error(), or req.log.debug() to emit structured logs with the correct request metadata (requestId, correlationId, route, tenant, and actor) automatically populated. This resolves workarounds where context was lost during event loop execution.
-
Request Logger Types & Helpers (
src/types/express.d.ts,src/utils/logger.ts):- Augment Express
Requesttype definition to expose thelog: RequestLoggerproperty. - Implement
createRequestLoggerinlogger.tsto format logs with a custom/explicit contextMap. - Update
loggermethods (info,warn,error,debug) to acceptredactionContextparameters.
- Augment Express
-
Middleware Attachment (
src/middleware/requestId.ts):- Instantiate and bind
req.logto a Proxy wrapper over the request tracing context. - The Proxy implements dynamic fallback lookup for
tenantandactorIDs from the request header/user properties, allowing downstream auth middlewares to cleanly update the logging context.
- Instantiate and bind
-
ESLint Plugin Update (
src/observability/eslint-plugin-logger-schema.ts):- Modified the custom ESLint rules to parse and validate both
logger.x()andreq.log.x()expressions. This ensures thatreq.logcalls conform to the allowlist PII schema validations.
- Modified the custom ESLint rules to parse and validate both
-
Documentation:
- Updated
README.md,docs/LOGGING.md, anddocs/observability.mdto document the newreq.loglogger, including example usages.
- Updated
-
Tests:
- Added
src/middleware/__tests__/requestLogger.test.tsto test request context binding, dynamic updating of tenant/actor context, and deferred logging outside request lifecycles.
- Added
Closes #866