Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Ability to modify log message #149

Open
mvastola opened this issue Jan 16, 2022 · 4 comments
Open

[Feature Request] Ability to modify log message #149

mvastola opened this issue Jan 16, 2022 · 4 comments

Comments

@mvastola
Copy link

mvastola commented Jan 16, 2022

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 mvastola changed the title [Feature Request] Ability to modify what is logged before logging [Feature Request] Ability to modify log message Jan 16, 2022
@reidmorrison
Copy link
Owner

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
Copy link
Author

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
Copy link

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants