rails_semantic_logger
rails_semantic_logger copied to clipboard
ActiveSupport::Logger getting "hijacked" by Rails Semantic Logger?
This is more of a question ... We're not entirely sure what's happening under the hood here, but it feels strange.
Environment
- Ruby 3.0.2
- Rails Version 6.1.4.1
- Semantic Logger Version 4.8.1
- Rails Semantic Logger Version 4.6.1
- Other Application/framework names and versions (e.g. Puma, etc.). Webpacker 5.4.3
Hope this image explains the issue?

This was initially reported on https://github.com/rails/webpacker/issues/3208
It seems like rails semantic logger "hijacks" ActiveSupport::Logger in some way, and then when Webpacker uses ActiveSupport::Logger.new(STDOUT), it doesn't return an ActiveSupport::Logger instance, and the Rails semantic logger instance being returned does not actually log to stdout ?
Expected Behavior
Logs to stdout
Actual Behavior
Does not log to stdout.
Semantic Logger is as clean as I can make it, but Rails Semantic Logger is nothing but a huge ugly hack to try and get Rails to log in a Semantic (machine readable) way. All of the hackery goes on in here: https://github.com/reidmorrison/rails_semantic_logger/blob/master/lib/rails_semantic_logger/engine.rb
If Rails adopted Semantic Logger directly we could just delete Rails Semantic Logger entirely.
To fix the stdout logging problems with Rails, we used this hack: https://github.com/reidmorrison/rails_semantic_logger/blob/master/lib/rails_semantic_logger/extensions/rails/server.rb
A similar patch for Web Packer would send all log messages to the right place.
Thank you @reidmorrison. It's hard for me to say, since I'm not deeply familiar with ruby logging or the rails internals. Have you considered submitting a PR to improve the Rails logging? I'm sure the community can benefit from your experience. Perhaps you can also propose integrating SemanticLogger into Rails? I'd be happy to "vouch" for it, FWIW :)
Wishing you a happy holiday season and a very happy and healthy 2022. Thanks for contributing your time and energy into open-source. I know it's not easy and not everyone is grateful, but I certainly appreciate your work, and the work of many other open-source contributors. I also try to contribute where I can, on my "own" projects and others like SemanticLogger. If there's anything I can do more to help, please let me know and I'll do my best to help.
Not sure if it helps, but Rails logs now to STDOUT by default, see https://github.com/rails/rails/pull/47138.
Could we modify the following lines, to provide a different format and logger?
config.logger = ActiveSupport::Logger.new(STDOUT)
.tap { |logger| logger.formatter = ::Logger::Formatter.new }
.then { |logger| ActiveSupport::TaggedLogging.new(logger) }