I'm currently using NestJS-Pino as a logging library in my Nest.js application. I have configured a middleware that is supposed to log requests, and I'd like it to show logs that are of the pino-http type.
Here is an example of the log format that I want (recieved from app controller):
{"level":30,"time":epochtime,"pid":pid,"hostname":"local","req":{"id":"someid","method":"GET","url":"/","query":{},"params":{"0":""},"headers":{"user-agent":"useragent","accept":"*/*","host":"localhost:3000","accept-encoding":"gzip, deflate, br","connection":"keep-alive"},"remoteAddress":"::1","remotePort":57377},"context":"AppController","msg":"Request..."}
However, I'm only getting a log with this format (non pino-http):
{"level":30,"time":epochtime,"pid":pid,"hostname":"local","msg":"Request..."}
My AppModule is defined as follows:
@Module({
imports: [
LoggerModule.forRoot({
pinoHttp: {
autoLogging: false,
},
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(LoggerMiddleware).forRoutes('*');
}
}
And my LoggerMiddleware is as follows:
@Injectable()
export class LoggerMiddleware implements NestMiddleware {
private readonly logger = new Logger(LoggerMiddleware.name);
use(req: Request, res: Response, next: NextFunction) {
this.logger.log('Request...');
next();
}
}
What do I need to change in order to get logs in the Pino-HTTP format to work like they do in the app service and controller? Thanks in advance
Try
this.logger.log({ req }, 'Request...');Check out docs !