Skip to content

Commit

Permalink
IMN 473 - Replace express winston middleware (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
MalpenZibo authored May 23, 2024
1 parent 0570e6e commit 0fcd80c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/agreement-process/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
authenticationMiddleware,
contextMiddleware,
loggerMiddleware,
zodiosCtx,
} from "pagopa-interop-commons";
import healthRouter from "./routers/HealthRouter.js";
Expand All @@ -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;
2 changes: 2 additions & 0 deletions packages/attribute-registry-process/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
authenticationMiddleware,
contextMiddleware,
loggerMiddleware,
zodiosCtx,
} from "pagopa-interop-commons";
import attributeRouter from "./routers/AttributeRouter.js";
Expand All @@ -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;
2 changes: 2 additions & 0 deletions packages/catalog-process/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
authenticationMiddleware,
contextMiddleware,
loggerMiddleware,
zodiosCtx,
} from "pagopa-interop-commons";
import eservicesRouter from "./routers/EServiceRouter.js";
Expand All @@ -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;
27 changes: 27 additions & 0 deletions packages/commons/src/logging/loggerMiddleware.ts
Original file line number Diff line number Diff line change
@@ -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();
};
}
2 changes: 2 additions & 0 deletions packages/purpose-process/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
authenticationMiddleware,
contextMiddleware,
loggerMiddleware,
zodiosCtx,
} from "pagopa-interop-commons";
import purposeRouter from "./routers/PurposeRouter.js";
Expand All @@ -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;
2 changes: 2 additions & 0 deletions packages/tenant-process/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
authenticationMiddleware,
contextMiddleware,
loggerMiddleware,
zodiosCtx,
} from "pagopa-interop-commons";
import healthRouter from "./routers/HealthRouter.js";
Expand All @@ -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;

0 comments on commit 0fcd80c

Please sign in to comment.