-
EnvironmentProvide at least:
With regard to the Name Tags:
I want to add user_id to every log message config.log_tags = {
user_id: -> controller.current_user_id.present? "#{controller.current_user_id}" : 'guest'
} 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. 🤔 |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
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'] }
} |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
Ah thanks! By implementing Also written in our doc:
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!!! |
Beta Was this translation helpful? Give feedback.
-
link https://rocketjob.github.io/semantic_logger/rails.html is broken |
Beta Was this translation helpful? Give feedback.
-
This is the latest url: https://logger.rocketjob.io/rails.html |
Beta Was this translation helpful? Give feedback.
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:
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!!!