-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
How can I add a field to all logs in the same request? #360
Comments
Take a look at https://www.fastify.io/docs/latest/Logging/. You can use the logs |
Thanks for the pointer @mcollina, but I'm not sure I can use serializers or formatters to achieve what I want:
The solution I'm currently considering involves replacing
Can you think of a better way to achieve what I want? |
That's it, yes. I think you should set it on reply as well. |
Thanks @mcollina! I'm making progress with the hook below, but unfortunately is seems to be run after the await server.addHook('onRequest', (request, reply, done) => {
if (typeof request.id === 'string') {
const [trace, spanId] = request.id.split(';')[0].split('/');
const bindings = { trace: `projects/relaycorp-cloud-gateway/traces/${trace}`, spanId };
request.log = request.log.child(bindings);
reply.log = reply.log.child(bindings);
}
done();
}); |
You can use https://www.fastify.io/docs/latest/Server/#disablerequestlogging and disable all native logging... and then implement your own in hooks. |
Thanks! I'll do that. I'll close this issue now, since the rest of the work seems straightforward. |
It would be good if you'd like to contribute a little guide or a plugin to do that, as it's a question that pops up very often! |
Indeed, that's what I'm planning to do once I get it all to work. I'm planning to create a cloud-agnostic plugin, so that other folks can add support for AWS and others. I'll keep you posted! |
This reverts commit ad8c8a5.
You have already researched for similar issues?
Yes
What are you trying to achieve or the steps to reproduce?
I want all the logs in any given request to have some fields that originates in a request header. That includes the logs that Fastify writes when the request starts and ends.
The reason why I want to do this is so that I can map log entries to traces on Google Cloud Trace. I basically want to parse the
X-Cloud-Trace-Context
request header, to extract two fields that I'll then add to all logs.In other words, I'd like to do something like
request.log.child({field1, field2})
, but changingrequest.log
in place in the context of the current request only -- I don't want to introduce any side effects.What was the result you received?
I don't know how to do what I'm trying to do.
What did you expect?
Context
The text was updated successfully, but these errors were encountered: