Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request-scoped logging #990

Closed
macourteau opened this issue Jan 27, 2024 · 3 comments
Closed

Request-scoped logging #990

macourteau opened this issue Jan 27, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@macourteau
Copy link

Fastify includes a log member in the request, which makes it possible to output request-scoped logs. Specifically, the output logs include a reqId which is unique to every request, but consistent across all logs generated within the scope of a request, making it possible to better analyze logs later.

It would be very useful to be able to access this logger from within request handlers. I presume the logger could output non-request-scoped logs in other plugins if the system they integrate into doesn't have request-scoped logging (e.g. connect-express, AFAICT).

This would be useful in Connect for Node.js - I don't think it would apply to Connect for Web.

I have tried to capture the log member from the Fastify object and use it within request handlers, but the logs generated by that logger are not request-scoped.

@macourteau macourteau added the enhancement New feature or request label Jan 27, 2024
@smaye81
Copy link
Member

smaye81 commented Jan 27, 2024

Hi @macourteau you can do this now using context. Take a look at #835 for an example of how to do it. Specifically this comment.

Let me know if that helps.

@macourteau
Copy link
Author

Ah, yes, that absolutely works - thanks! I think it would be useful to add this to the documentation, perhaps in the Context section, or an "Advanced" section.

@smaye81
Copy link
Member

smaye81 commented Jan 29, 2024

Totally agree @macourteau . We are actually updating the docs to include this, they're just still in-flight. You can see the PR here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants