rails_semantic_logger icon indicating copy to clipboard operation
rails_semantic_logger copied to clipboard

[Feature Request] Ability to modify log message

Open mvastola opened this issue 3 years ago • 4 comments
trafficstars

Is there any way you could add the ability to override the message that is logged with given events? (For example, in lieu of Completed ##{payload[:action]})

A simple implementation could be to replace the message with one from the payload if set at a certain key.

Unfortunately, it's not easy to do otherwise via monkeypatching or similar because blocks (instead of functions) are used to generate log data (example).

Environment

  • Ruby Version. 2.7.2
  • Rails Version. 6.1.4.4
  • Semantic Logger Version. 4.9.0
  • Rails Semantic Logger Version. 4.9.0
  • Other Application/framework names and versions (e.g. Puma, etc.). Not relevant
  • Rails configuration. Only need the settings related to Rails Semantic Logger and Semantic Logger. Not relevant
  • Full Stack Trace, if an exception is being raised. Not applicable

mvastola avatar Jan 16 '22 07:01 mvastola

Yes, the same way Rails Semantic Logger swapped out the Rails built-in log subscriber, you can swap out the Rails Semantic Logger built-in log subscriber with your own.

https://github.com/reidmorrison/rails_semantic_logger/blob/master/lib/rails_semantic_logger/engine.rb#L196

Rather than changing the message itself, other developers add log tags to every request, using config.log_tags, or use the builtin Rails method append_info_to_payload to add controller specific log tags to your output?

reidmorrison avatar Jan 17 '22 20:01 reidmorrison

I know the LogSubscriber can be replaced, but that would require duplicating all the other features of this gem, which are great.

And yes, log tags are very helpful, and I do plan to use them, but we aggregate logs from many different sources and we usually just show the message and then you can click the entry to view details. I was hoping there could be some way to make the message more informative.

mvastola avatar Jan 17 '22 21:01 mvastola

I have a similar issue. My use case is Rails running in Docker with a read-only filesystem. This runs in Google Cloud Platform so logs sent to stdout are collected in Cloud Logging automatically, and Cloud Logging likes fluentd formatting so I send logs formatted as :fluentd.

When I view the logs in Cloud Logging all I see is "Completed #show" until I click on it to show the jsonPayload. It would be great if I could say "set the message field to the output of :default and set the jsonPayload field to the output of :fluentd" so that I get the best of both worlds: a one-line text string that I can read at a glance and the structured JSON data that makes the logs easier to query.

I hope this helps illustrate the utility of customizing message. I know I could define my own formatter but it would be nice to have as a configurable option.

anothermh avatar Mar 22 '22 17:03 anothermh

Sounds like a common need, anyone want to submit a pull request with a solution?

reidmorrison avatar Mar 24 '22 13:03 reidmorrison