-
Notifications
You must be signed in to change notification settings - Fork 98
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
[QUESTION] Log context inside middlewares #1096
Comments
must be middlewares order. debug or just add console.log() in your middleware and |
You were right @iamolegga, it was that. Thanks! |
Yep, definitely this need to be added to FAQ |
Hi, I'm facing a similar issue. Could you pls help me understand how to // app.module.ts
@Module({
imports: [
CoreModule,
CatsModule,
LoggerModule.forRoot(
{
pinoHttp: {
autoLogging: false,
quietReqLogger: true,
}
}
),
],
})
export class AppModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(FoobarMiddleware).forRoutes("*");
}
} // main.ts
async function bootstrap() {
const app = await NestFactory.create(AppModule, { bufferLogs: true });
app.useLogger(app.get(Logger));
app.useGlobalPipes(new ValidationPipe());
await app.listen(3000);
console.log(`Application is running on: ${await app.getUrl()}`);
}
bootstrap(); // foobar.middleware.ts
@Injectable()
export class FoobarMiddleware implements NestMiddleware {
constructor(
@InjectPinoLogger(FoobarMiddleware.name)
private logger: PinoLogger,
) {
console.log("foobar middleware constructor bindings", this.logger.logger?.bindings());
}
use(req: any, res: any, next: () => void) {
this.logger.info("We want to have reqId in this log."); // <------ reqId is absent in the middleware logging
next();
}
} |
move |
I tried update the imports order in AppModule, but no luck. @Module({
imports: [
LoggerModule.forRoot( // <----- LoggerModule is the first in the imports array
{
pinoHttp: {
autoLogging: false,
quietReqLogger: true,
}
}
),
CoreModule,
CatsModule,
],
})
export class AppModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(FoobarMiddleware).forRoutes("*");
}
} Is any other change needed? |
move |
That works! Thank you! @Module({
providers: [
...
],
})
export class CoreModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(FoobarMiddleware).forRoutes("*"); // <---- middleware moved to here (was in AppModule)
}
} |
[ 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
Is there any way to get a working log context inside nestjs (or even raw express) middlewares? The code above just get the request id (from http-pino's
genReqId
) for theResponse sent
log, whileRequest received
remains with no context information.Already tried some workarounds but none of them is working properly. Maybe could be related to #803 (not sure)
This middleware is the first one loaded on nestjs request lifecycle. I can't use interceptor for that because I need to log request/response considering other application middlewares as well, and interceptors are executed after all middlewares. Any suggestions?
Please mention other relevant information such as Node.js version and Operating System.
The text was updated successfully, but these errors were encountered: