reidmorrison / rails_semantic_logger

Rails Semantic Logger replaces the Rails default logger with Semantic Logger
https://logger.rocketjob.io/rails
Apache License 2.0
311 stars 115 forks source link

[Feature Request] Ability to modify log message #149

Open mvastola opened 2 years ago

mvastola commented 2 years ago

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

reidmorrison commented 2 years ago

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?

mvastola commented 2 years ago

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.

anothermh commented 2 years ago

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.

reidmorrison commented 2 years ago

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