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.

[FEATURE REQUEST] Logger usage outside HTTP context

See original GitHub issue

Question

I’m trying to use logger.assign outside of HTTP context. Specifically, inside sqs consumer’s handleMessage function.

I figured out with the help of #788 to initiate the storage.run on my own. The problem was that i don’t have access to req.log so i used PinoLogger.root instead. Is this supposed to be used like this?

import { storage, Store } from 'nestjs-pino/storage';
import { PinoLogger } from 'nestjs-pino';

---

    this.consumer = Consumer.create({
      // this magic creates AsyncLocalStorage context
      // for nestjs-pino to work with. It allows the use
      // of this.logger.assign() method.
      handleMessage: message =>
        storage.run(new Store(PinoLogger.root), () =>
          this.processMessage(message),
        ),
    });

It seems to work fine. But want to make sure we won’t break something in strange ways.

I guess this also relates to @nestjs/bull and @nestjs/microservices are those environments supported? As they are similarly not inside HTTP context (not passing middlewares afaik).

Edit: If this is a valid usage, maybe it could be wrapped in a nicer withNestPinoContext(() => this.processMessage(message)) like function.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:13
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

6reactions
iamoleggacommented, Aug 3, 2022

OK, in next month or two I’m going to investigate if it’s possible to make such feature work with different microservices’ transports including custom strategies.

For implementing this feature in a clear way we need middlewares for microservices to be landed in nest. Until then this solution could be used.

2reactions
iamoleggacommented, Feb 5, 2022

Desired behavior is not supported at the moment. You can try any hacks (as presented above) to make it work but on your own risk. This is not tested and not supported.

For this feature we need something similar to pino-http-like settings with a lot of settings beside pino-related ones. So this is huge feature and it need a lot of time for implementation. Unfortunately I’m not able to invest in this at the moment. But maybe this year on my current project we will need such feature too, then I think we can invest some time for this. But that’s not 100%. In case anyone would like to implement this, feel free to open detailed RFC for this first, before opening PR.

Will change this issue to feature request.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · iamolegga/nestjs-pino - GitHub
Platform agnostic logger for NestJS based on Pino with REQUEST CONTEXT IN EVERY LOG - Issues ... [FEATURE REQUEST] Logger usage outside HTTP...
Read more >
Use HttpContext in ASP.NET Core - Microsoft Learn
In this article. HttpContext encapsulates all information about an individual HTTP request and response. An HttpContext instance is initialized ...
Read more >
Retrieving HttpContext in a Custom NLog Target
So, I want to store this Guid in HttpContext. Current. Items , then retrieve it in the NLog target and include it in...
Read more >
How to log request & response body to Application Insights
Writing a custom middleware ; class RequestBodyLoggingMiddleware ; var method = context.Request.Method; // Ensure the request body can be read ...
Read more >
ASP.NET Core request logging middleware - elmah.io Blog
Usually, when getting errors logged through MEL, all of the HTTP context data is logged as part of the error, making it easy...
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