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
Fix the BroadcastLogger being initialized too late: #49417
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -56,6 +56,10 @@ module Bootstrap | |||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||
Rails.logger.level = ActiveSupport::Logger.const_get(config.log_level.to_s.upcase) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
broadcast_logger = ActiveSupport::BroadcastLogger.new(Rails.logger) | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible that when users already use (I deleted my original comment on the commit as a PR comment should be more discoverable) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👋 That's indeed possible, and I remember seeing this in applications frequently. I made sure that it is supported rails/activesupport/test/broadcast_logger_test.rb Lines 163 to 175 in a5f113f
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I wasn't clear. I didn't mean that the propagation wouldn't work. I meant the logger would be nested like this:
If the user manually configs broadcast logger like: config.logger = ActiveSupport::BroadcastLogger.new(
::Logger.new(nil),
::Logger.new(nil)
) I personally would be surprised if I see another single There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah sorry I misunderstood. Yeah good point, that feels wrong. I'll open a fix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think related to this, I'm noticing an issue where the initially configured logger is being put into the broadcast_logger = ActiveSupport::BroadcastLogger.new(
config.logger # <-- Logs via LogSubscriber are being logged to this logger and not others
) I haven't been able to write a test that reproduces the behavior, so it could be a strange configuration in my own codebase. I thought it was worth calling out there anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This issue is what this PR fixes, so maybe you are using a version without this fix? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also check if other loggers (e.g. To help debug you could create 2 initializers and see the value of # config/application.rb
initializer "debug_logger_before", before: "action_controller.set_configs" do
puts ActionController::Base.logger # Should normally be nil
end
initializer "debug_logger_after", after: "action_controller.set_configs" do
puts ActionController::Base.logger # Should be a `BroadcastLogger` instance.
# If it's a `BroadcastLogger` instance, then something in your app or a gem
# modifies it afterward.
# If it's not a `BroadcastLogger`, it could be because the logger is overriden.
# Check if you have a line of code that looks like `config.action_controller.logger = ...`
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was very helpful, thank you! To confirm, it was an issue on our end, but not the way I thought it was. We had a test that was stubbing We updated our test to stub the correct method and it's working. Thanks again for this debugging help! |
||||||||||||||||||||||||||||
broadcast_logger.formatter = Rails.logger.formatter | ||||||||||||||||||||||||||||
Rails.logger = broadcast_logger | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
unless config.consider_all_requests_local | ||||||||||||||||||||||||||||
Rails.error.logger = Rails.logger | ||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method was not working properly even before. It was only checking if the main logger (the broadcast one) would output to
sources
but didn't consider that other loggers part of the broadcast could have.