How to log correlation id with Sequelize?

Viewed 12

My REST API server contains CRUD endpoints for User entity that is persisted into a DB using Sequelize. Each endpoint generates a uuid v4 correlation id that is supposed to be present in every log message. I am using pino and child loggers with correlation id provided as binding options so the dev does not have to worry or even be aware of any correlation id nor the requirement to provide it in every log message (the dev just logs using a logger in a particular context (rest endpoint handler)).

Is there any way to inject correlation id or a child logger into Sequelize to log Sequelize queries with a different correlation id in different contexts (rest endpoint handlers)?

I do not want to provide a logger fn in every Sequelize model operation (not practical / tedious / error prone).

I'd like to achieve something like this:

1. rest handler is called // this is the start of the execution context
2. correlation id CID is generated and injected into logger and ???somehow into Sequelize??? // CID is constant from the perspective of the current execution context
3. some business logic // logs every log message with CID without explicitly specifying it
4. await User.findByPK(id) // I do not want to pass CID in here as it should be clear from the execution context
5. a log message is produced containing CID
6. some business logic // logs every log message with CID without explicitly specifying it
7. rest handler finished // end of the execution context

Note: By "injected into logger" I mean that a child logger is created with binding options to ensure that logging properties do not get mixed up.

0 Answers
Related