-
Notifications
You must be signed in to change notification settings - Fork 116
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
access request ID outside middleware #274
Comments
Not sure if you've found the solution yet, but you can try |
Thanks @hliang I was able to resolve this based on your comment. In order to get it to work, instead of using the Logger from constructor(
private readonly logger: PinoLogger,
) {
this.logger.setContext(RequestLoggerInterceptor.name);
}
async myFunc() {
const requestId = this.logger?.logger?.bindings()?.req?.id;
} and I was able to control the req.id in the initial injection of the logger in the app.module.ts (docs for genReqId) LoggerModule.forRootAsync({
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
pinoHttp: {
genReqId: function (req, res) {
const existingID = req.id ?? req.headers['x-request-id'];
if (existingID) return existingID;
console.log(`found an id: ${existingID}`);
const id = uuidv4();
res.setHeader('X-Request-Id', id);
console.log(`created an id: ${id}`);
return id;
},
...otherConfig
},
}),
}), |
You could implement something like https://github.com/mcollina/fastify-asyncforge. |
Actually, I'm saving activity logs in my database and I also want to save the associated "reqId" so I can track down problems later if any arise. I need to access the log request ID outside the
logger.log
function. I also mentioned the scenario below:app.module.ts
app.service.ts
The text was updated successfully, but these errors were encountered: