question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[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:closed
  • Created 2 years ago
  • Comments:25 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
iamoleggacommented, Dec 23, 2021

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 with storage.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.

2reactions
itheodorocommented, Jan 20, 2022

Closing this issue since @nestjs/platform-fastify@8.2.6 fixes the wrong behavior by updating fastify version. Thanks all!

Read more comments on GitHub >

github_iconTop Results From Across the Web

fastify-request-context is not working for POST API calls
This is my fastify server written in typescript while using the fastify-request-context plugin I am able to access the context of fastify ...
Read more >
Usage with Fastify - tRPC
The best way to start with the Fastify adapter is to take a look at the example ... Then you need a context...
Read more >
Testing | NestJS - A progressive Node.js framework
We simulate HTTP tests using the request() function from Supertest. We want these HTTP requests to route to our running Nest app, so...
Read more >
Logging - Fastify
As Fastify is focused on performance, it uses pino as its logger, with the default log level, when enabled, set to 'info' ....
Read more >
nest could not find configservice element (this provider does ...
bug report ; feature request; documentation issue. nestjs-config version v1.4.5. @nestjs/common+core or other package versions.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found