Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IMN 473 - Replace express winston middleware #434

Merged
merged 8 commits into from
May 23, 2024

Conversation

MalpenZibo
Copy link
Collaborator

@MalpenZibo MalpenZibo commented Apr 18, 2024

Re-add the logger middleware removed here #423

NOTE:
With this new logger middleware, we're unable to add a log to the /health request

image

NEW format
image

@MalpenZibo MalpenZibo force-pushed the IMN-473_replace-express-winston-middleware branch from dd7efdc to b0f66ab Compare April 18, 2024 15:18
@MalpenZibo MalpenZibo changed the base branch from main to IMN-473_remove-global-storage April 18, 2024 15:26
@MalpenZibo MalpenZibo force-pushed the IMN-473_remove-global-storage branch 3 times, most recently from 94a4646 to dc7a122 Compare May 2, 2024 10:42
Base automatically changed from IMN-473_remove-global-storage to main May 8, 2024 12:35
@MalpenZibo MalpenZibo force-pushed the IMN-473_replace-express-winston-middleware branch from b0f66ab to 623a280 Compare May 13, 2024 13:01
Comment on lines 17 to 19
loggerInstance.info(`Request ${req.method} ${req.url}`);
res.on("finish", () => {
loggerInstance.info(`Response ${res.statusCode} ${res.statusMessage}`);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we have to decide what we want to log.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we log the same info that we were logging with the previous middleware?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old configuration was temporary so we could decide here the final version

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few considerations:

  • I like the idea of having one log before and one after the call, it's easier to read. On the other side, this would reflect in a 50% log increase for the GETs, which are the most frequent called endpoints. Since Cloudwatch costs are not negligible, and considering that the double log does not actually add information, I would keep only the log at the request completion
  • considering that the logger is already adding all context details, we can keep the previous log format
    Request {method} {urlWithParameters} - Response {code}
    Notes:
    • we need to include query and path parameters but not elements in the body, which can be sensitive
    • the previous log includes also the response status message. It does not actually add any information and we could remove it, but I've not strong opinions on this

cc @beetlecrunch

@MalpenZibo MalpenZibo marked this pull request as ready for review May 14, 2024 09:45
Copy link
Collaborator

@ecamellini ecamellini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM as long as we log the same info we were logging with the previous solution (or agree on a new set of info to log)

Comment on lines 6 to 13
export function loggerMiddleware(serviceName: string) {
return (req: any, res: any, next: express.NextFunction): void => {
const loggerMetadata: LoggerMetadata = {
serviceName,
userId: req.ctx?.authData?.userId,
organizationId: req.ctx?.authData?.organizationId,
correlationId: req.ctx?.correlationId,
};
Copy link
Contributor

@Carminepo2 Carminepo2 May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we remove the anys?
My proposal:

Suggested change
export function loggerMiddleware(serviceName: string) {
return (req: any, res: any, next: express.NextFunction): void => {
const loggerMetadata: LoggerMetadata = {
serviceName,
userId: req.ctx?.authData?.userId,
organizationId: req.ctx?.authData?.organizationId,
correlationId: req.ctx?.correlationId,
};
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,
};

This way, if the AppContext changes we would be "notified" by typescript at compile time

@MalpenZibo MalpenZibo merged commit 0fcd80c into main May 23, 2024
8 checks passed
@MalpenZibo MalpenZibo deleted the IMN-473_replace-express-winston-middleware branch May 23, 2024 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants