[FEATURE REQUEST] Logger usage outside HTTP context
See original GitHub issue- I’ve read the docs of nestjs-pino
- I’ve read the docs of pino
- I couldn’t find the same question about nestjs-pino
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:
- Created 2 years ago
- Reactions:13
- Comments:17 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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 besidepino
-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.