-
Notifications
You must be signed in to change notification settings - Fork 597
Customize the Logger metadata key name #1267
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
Customize the Logger metadata key name #1267
Conversation
In certain scenarios we may want to use the Plug.RequestID multiple times with different configuration options (maybe different headers or different assigns). While this is possible, the issue is that every call adds a `:request_id` key to the Logger metadata. When using the plug muliple times, the last call will override the metadata of the previous one. This commit adds a new `:log_as` option that allows users to customize how the request ID will be added to the logger metadata. If not provided, this option defaults to `:request_id` for backwards compatibility.
It is worth noting that I initially planned to use the same key as the At the end I decided against it because it could break existing applications that were using a custom |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a nice addition. 👍 Left a couple comments first.
lib/plug/request_id.ex
Outdated
@@ -47,6 +47,12 @@ defmodule Plug.RequestId do | |||
|
|||
plug Plug.RequestId, assign_as: :plug_request_id | |||
|
|||
* `:log_as` - The name of the key that will be used to store the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would name this :logger_metadata_key
.
lib/plug/request_id.ex
Outdated
@@ -47,6 +47,12 @@ defmodule Plug.RequestId do | |||
|
|||
plug Plug.RequestId, assign_as: :plug_request_id | |||
|
|||
* `:log_as` - The name of the key that will be used to store the | |||
discovered or generated request id in `Logger` metadata. If not provided, | |||
the request id will be stored as `:request_id`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the request id will be stored as `:request_id`. | |
the request ID Logger metadata key defaults to `:request_id`. |
lib/plug/request_id.ex
Outdated
discovered or generated request id in `Logger` metadata. If not provided, | ||
the request id will be stored as `:request_id`. | ||
|
||
plug Plug.RequestId, log_as: :my_request_id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plug Plug.RequestId, log_as: :my_request_id | |
plug Plug.RequestId, log_as: :my_request_id | |
*Available since v1.18.0*. |
Thanks for the feedback @whatyouhide! 🙇♂️ I've applied your suggestions and this is ready for another review. |
💚 💙 💜 💛 ❤️ |
Thanks for this addition 🎉 It just so happened that I had to roll my own plug to do this a few days ago, roughly at the time when this was being submitted :) Crazy timing! FWIW, here's my use-case for this. Using this makes it possible to link all HTTP requests made by browser during a test in Playwright - with logs:
In other words, after running a Playwright suite, it's now possible to filter out all entries logged during a specific test. |
In certain scenarios we may want to use the
Plug.RequestID
plug multiple times with different headers or assigns.While this is possible, the issue is that every call adds a
:request_id
key to the Logger metadata. When using the plug multiple times, the last call will override the metadata of the previous one.This commit adds a new
:logger_metadata_key
option that allows users to customize how the request ID will be added to the logger metadata. If not provided, this option defaults to:request_id
for backwards compatibility.Previous code could be modified as: