Logcraft.within_log_context not changing AccessLog context
Closed this issue · 2 comments
Description
Adding extra context to logs via Logcraft.within_log_context
works on the "Application" logger but not on the "AccessLog" logger.
How Has This Been Tested?
With this configuration in the app's ApplicationController
:
class ApplicationController < ActionController::Base
around_action :set_log_context
private
def set_log_context
context = {
foo: "bar"
}
Logcraft.within_log_context(context) { yield }
end
end
Expected to have the "foo": "bar"
context on every log resulting from a request, but it only shows up on logs from the "Application" logger. Logcraft.within_log_context
appears to not modify the context of the "AccessLog" logger.
{"timestamp":"2022-07-18T21:04:03.218-03:00","level":"DEBUG","logger":"Application","hostname":"8bdc5a40963a","pid":605,"request_id":"7ca69bc3-7c38-46c4-90c1-f184dc2ef743","foo":"bar","message":"ETHON: Libcurl initialized"}
{"timestamp":"2022-07-18T21:04:04.259-03:00","level":"INFO","logger":"AccessLog","hostname":"8bdc5a40963a","pid":605,"message":"GET /catalog/products - 304 (Not Modified)","remote_ip":"172.20.0.1","method":"GET","path":"/catalog/products","params":{"controller":"catalog/products","action":"index"},"response_status_code":304,"duration":1084,"duration_sec":1.084,"request_id":"7ca69bc3-7c38-46c4-90c1-f184dc2ef743"}
Workaround
None that I could find.
Versions
- Ruby 3.1.2
- Rails 7.0.3.1
- Logcraft (
main
branch @e40b66b66818226f107944b466d19250971a204e
)
Hi @rodrigotassinari ,
This is to be expected. The reason is that the AccessLog is a middleware deep inside Rails's middleware chain so by the time your code execution gets to a controller, you're already "inside" the AccessLog middleware - any context you start will be closed by the time execution gets back to the AccessLog, so your context data will have been removed already.
There are a couple of things you can do:
- Use the
global_context
configuration setting to inject data into the log. - Write a middleware to set your log context and place it before the Logcraft's
RequestLogger
in the chain.
I hope this helps.
Cheers,
Z
Thanks for the response @zormandi (and sorry about not replying sooner). This makes perfect sense. I'll use the global_context
then.