[QUESTION] PinoLogger: unable to assign extra fields out of request scope // trying to assign fields from AuthGuard
See original GitHub issue[x] I’ve read the docs of nestjs-pino
[x] I’ve read the docs of pino
[x] I couldn’t find the same question about nestjs-pino
Question Hello guys, we require to extend our logs with user data which get set to the REQ object by using a nestjs auth guard. Sadly those are not getting into the logs. I’m now trying to set the fields specifically using the “assign” method. But as it seems (and which makes sense) it is not possible due to the auth guard not beeing request scoped. How can we get our request context based information into the logs in a uniform way (we need to provide this from a libary, ideally in form of a middleware) because we require this in a lot of different microservices. I’ve tried writing a request scoped middleware but it gives the same error:
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Response, NextFunction } from 'express';
import { InjectPinoLogger, PinoLogger } from 'nestjs-pino';
import { UserRequest } from '@triluxds/tds-lp-nest-utilities';
@Injectable({ scope: Scope.REQUEST })
export class LoggerContextMiddleware implements NestMiddleware {
constructor(
@InjectPinoLogger(LoggerContextMiddleware.name)
private readonly logger: PinoLogger,
) {}
use(req: UserRequest, res: Response, next: NextFunction) {
this.logger.assign({ userId: req.user?.userId });
this.logger.assign({ customerId: req.user?.customerId });
this.logger.assign({ clientId: req.user?.clientId });
next();
}
}
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(LoggerContextMiddleware).forRoutes('*');
}
}
Please mention other relevant information such as Node.js version and Operating System.
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
i’ve opened a bug report so I’m closing this question
check the order of modules then.If you believe this is a bug feel free to open a bug report with minimal example