diff --git a/packages/agreement-process/src/app.ts b/packages/agreement-process/src/app.ts index 20fa157780..70f3d64f5c 100644 --- a/packages/agreement-process/src/app.ts +++ b/packages/agreement-process/src/app.ts @@ -1,6 +1,7 @@ import { authenticationMiddleware, contextMiddleware, + loggerMiddleware, zodiosCtx, } from "pagopa-interop-commons"; import healthRouter from "./routers/HealthRouter.js"; @@ -17,6 +18,7 @@ app.disable("x-powered-by"); app.use(contextMiddleware(serviceName)); app.use(healthRouter); app.use(authenticationMiddleware); +app.use(loggerMiddleware(serviceName)); app.use(agreementRouter(zodiosCtx)); export default app; diff --git a/packages/attribute-registry-process/src/app.ts b/packages/attribute-registry-process/src/app.ts index 9915af1f62..b90002124b 100644 --- a/packages/attribute-registry-process/src/app.ts +++ b/packages/attribute-registry-process/src/app.ts @@ -1,6 +1,7 @@ import { authenticationMiddleware, contextMiddleware, + loggerMiddleware, zodiosCtx, } from "pagopa-interop-commons"; import attributeRouter from "./routers/AttributeRouter.js"; @@ -17,6 +18,7 @@ app.disable("x-powered-by"); app.use(contextMiddleware(serviceName)); app.use(healthRouter); app.use(authenticationMiddleware); +app.use(loggerMiddleware(serviceName)); app.use(attributeRouter(zodiosCtx)); export default app; diff --git a/packages/catalog-process/src/app.ts b/packages/catalog-process/src/app.ts index 170a75b9f9..4751221527 100644 --- a/packages/catalog-process/src/app.ts +++ b/packages/catalog-process/src/app.ts @@ -1,6 +1,7 @@ import { authenticationMiddleware, contextMiddleware, + loggerMiddleware, zodiosCtx, } from "pagopa-interop-commons"; import eservicesRouter from "./routers/EServiceRouter.js"; @@ -17,6 +18,7 @@ app.disable("x-powered-by"); app.use(contextMiddleware(serviceName)); app.use(healthRouter); app.use(authenticationMiddleware); +app.use(loggerMiddleware(serviceName)); app.use(eservicesRouter(zodiosCtx)); export default app; diff --git a/packages/commons/src/logging/loggerMiddleware.ts b/packages/commons/src/logging/loggerMiddleware.ts index e69de29bb2..8b66c1bcc0 100644 --- a/packages/commons/src/logging/loggerMiddleware.ts +++ b/packages/commons/src/logging/loggerMiddleware.ts @@ -0,0 +1,27 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +import * as express from "express"; +import { AppContext } from "../context/context.js"; +import { LoggerMetadata, logger } from "./index.js"; + +export function loggerMiddleware(serviceName: string): express.RequestHandler { + return (req, res, next): void => { + const context = (req as express.Request & { ctx?: AppContext }).ctx; + + const loggerMetadata: LoggerMetadata = { + serviceName, + userId: context?.authData?.userId, + organizationId: context?.authData?.organizationId, + correlationId: context?.correlationId, + }; + + const loggerInstance = logger(loggerMetadata); + + res.on("finish", () => { + loggerInstance.info( + `Request ${req.method} ${req.url} - Response ${res.statusCode} ${res.statusMessage}` + ); + }); + + next(); + }; +} diff --git a/packages/purpose-process/src/app.ts b/packages/purpose-process/src/app.ts index 923919ed6c..e5f81b34bd 100644 --- a/packages/purpose-process/src/app.ts +++ b/packages/purpose-process/src/app.ts @@ -1,6 +1,7 @@ import { authenticationMiddleware, contextMiddleware, + loggerMiddleware, zodiosCtx, } from "pagopa-interop-commons"; import purposeRouter from "./routers/PurposeRouter.js"; @@ -17,6 +18,7 @@ app.disable("x-powered-by"); app.use(contextMiddleware(serviceName)); app.use(healthRouter); app.use(authenticationMiddleware); +app.use(loggerMiddleware(serviceName)); app.use(purposeRouter(zodiosCtx)); export default app; diff --git a/packages/tenant-process/src/app.ts b/packages/tenant-process/src/app.ts index 361b05c6a3..2bd5cf10d4 100644 --- a/packages/tenant-process/src/app.ts +++ b/packages/tenant-process/src/app.ts @@ -1,6 +1,7 @@ import { authenticationMiddleware, contextMiddleware, + loggerMiddleware, zodiosCtx, } from "pagopa-interop-commons"; import healthRouter from "./routers/HealthRouter.js"; @@ -17,6 +18,7 @@ app.disable("x-powered-by"); app.use(contextMiddleware(serviceName)); app.use(healthRouter); app.use(authenticationMiddleware); +app.use(loggerMiddleware(serviceName)); app.use(tenantRouter(zodiosCtx)); export default app;