Closed truongnmt closed 3 years ago
Unfortunately this is a limitation of the way Rails does log tags. In our application we use the following to workaround this limitation:
config.log_tags = {
request_id: :uuid,
ip: :remote_ip,
amzn_trace_id: -> request { request.headers["X-Amzn-Trace-ID"] },
incap_req_id: -> request { request.headers["INCAP-Req-ID"] },
login: -> request { request.cookie_jar['login'] }
}
log_tags are resolved long before the controller is created, so it is not possible to access the eventual controller at that point in time. You could use the Controller around feature to add named tags around all of the controller actions.
Ah thanks! By implementing append_info_to_payload
we could add custom data to its payload from any controller. Rails support this:
https://apidock.com/rails/v6.0.0/ActionController/Instrumentation/append_info_to_payload
Also written in our doc:
Adding custom data to the Rails Completed log message: https://rocketjob.github.io/semantic_logger/rails.html
Just make sure to implement this method in the base controller of all requests. That's very close and enough to my original purposes. Adding to log_tags seems a bit difficult, I should read the docs more carefully. Thanks very much!!!
link https://rocketjob.github.io/semantic_logger/rails.html is broken
This is the latest url: https://logger.rocketjob.io/rails.html
Environment
Provide at least:
With regard to the Name Tags:
I want to add user_id to every log message
However, it seems that I can't access the controller in there.
To access controller we can use payload but I want to add this information on every log. Not just custom data. It's something in between Named Tags (added on every log message) and Payload (controller accessible).
It would be nice if I can add an additional log to Rails's default system log. 🤔