[BUG] Request context not working when using FastifyAdapter
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 open issue of nestjs-pino
What is the current behavior?
I’m using NestJS with Fastify. nestjs-pino
works pretty well with HTTP GET requests and with other HTTP requests that doesn’t have a body. When requests have a body, the logger does not brings context and only prints the basic log message, even when log is invoked inside the request context.
What is the expected behavior? When logging inside a request context, all log entries should have the correct context.
- Correct log entry for request without body:
{"level":30,"time":1631560507217,"pid":448510,"hostname":"host","req":{"id":"req-1","method":"GET","url":"/","headers":{"content-type":"application/json","accept":"*/*","cache-control":"no-cache","host":"localhost:5000","accept-encoding":"gzip, deflate, br","connection":"keep-alive","content-length":"20"},"remoteAddress":"127.0.0.1","remotePort":35350},"context":"AppController","msg":"Hi from getHello"}
- Incorrect log entry for request with body:
{"level":30,"time":1631560533607,"pid":448510,"hostname":"host","context":"AppController","msg":"Hi from postHello"}
Please provide minimal example repo. Without it this issue will be closed Example repo with log in app.controller.ts
Please mention other relevant information such as Node.js version and Operating System. Node.js version: v14.17.6 OS: Ubuntu 20.04.3 LTS nestjs-pino version: ^2.2.0 @nestjs/platform-fastify version: ^8.0.6
Important to mention that the wrong behavior doesn’t occurs when using Nest with Express.
I also noticed a possible fix, but I don’t know if it could cause some issue since I’m not very familiar with AsyncLocalStorage. The possible fix consists in replace AsyncLocalStorage run
by AsyncLocalStorage enterWith
method in LoggerModule.
Thanks for creating such a helpful package! 🙏
Issue Analytics
- State:
- Created 2 years ago
- Comments:25 (8 by maintainers)
Top GitHub Comments
Thanks, but I think it’s enough of FYI’s here, it won’t help, only spam notifications, I’m already subscribed to all the related issues and watching the situation.
It looks like we only need to change the
storage.run
withstorage.enterWith
in the code. But that thing should be tested correctly, as current tests pass for fastify, so the main challenge here is to write correct tests for this first, check that current behavior is broken, and the new one is correct.Closing this issue since
@nestjs/platform-fastify@8.2.6
fixes the wrong behavior by updating fastify version. Thanks all!