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
Tags leak across broadcasted loggers #46084
Comments
@thomasgt correct me if I'm wrong, but in this example aren't you doing the opposite and sending broadcasts from tagged_logger2 to tagged_logger1?
|
You are creating two tagged loggers,
Now when you call Is this intentional? I'm not sure. There is a test ensuring |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
This is still a bug. I think it happens because of the way broadcasting extends the logger with a module that closes over the secondary logger, and then tagging later clones the logger, which can't clone the local variable trapped in the closure. So adding tags to the formatter ends up modifying the base logger's formatter. The idea in #44695 about "using composition" sounded right, but it's not without its own complexities. It means we need to carefully delegate a lot of methods in different ways. Methods that just get sent to the child loggers vs methods that take a block vs methods that return something (e.g., #formatter). It's probably doable, but seems a bit messy. I'm thinking a more fundamental problem is that broadcasting is implemented at the wrong level. It shouldn't wrap the logger, just the log device inside the logger. This isn't currently exposed by ruby's Logger, but maybe that can change (ruby/logger#88). That thing only has 3 methods: write, close, reopen, which could be much more easily reimplemented to delegate to the wrapped loggers' logdevs. |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
I'm working on refactoring TaggedLogging and implement a public API for broadcasting logs in #48615 and as I was adding a regression test to make sure this use case is taken in consideration. To summarise your use case in your test, this is a small schema. flowchart TD;
subgraph B2["Broadcasting loggers"]
direction LR
tagged_logger2["tagger_logger_2 (Has no tags applied)"]-- clone ---
final_logger["final_logger (Has the ['test'] tag applied)"]
end
tagged_logger2-->tagged_logger1
final_logger-->tagged_logger1
When you call This is what I would expect:
Your test case doesn't cover 2), but would that be what would expect in your logs? Thanks ❤️ ! |
- ### Context The Tagged Logging functionality has been a source of a few issues over the years, especially when combined with the broadcasting feature. Initializating a Tagged Logger wasn't very intuitive: ```ruby logger = Logger.new(STDOUT) tagged_logger = ActiveSupport::TaggedLogging.new(logger) # Would expect the `tagged_logger` to be an instance of `AS::TaggedLogging` # but it's a clone of the `logger`. tagged_logger.formatter = ->(_, _, _, message) do { message: message } end # Modifies the formatter to output JSON formatted logs. # This breaks tagged logging. ``` I believe the main reason of those issues is because tagged logging is implemented at the wrong level. ### Solution I made a proposal on the Ruby logger upstream in ruby/logger#90 to help solve this but it hasn't been reviewed yet. So I thought about adding it here for now. The TL;DR is to decouple formatting and adding extra information to logs (which is what tagged logging does). ### Deprecation Since TaggedLogging will no longer access the formatter, there is a few things I'd like to deprecate (such as setting a default formatter https://github.com/rails/rails/blob/d68e43922bc11829c52ad9f736ad5549fc97631b/activesupport/lib/active_support/tagged_logging.rb#L124) but doing so in this PR would increase the size of the diff significantly and would maybe distract for PR reviews. Another thing that I believe should be deprecated is `ActiveSupport::TaggedLogging.new`. Adding tagging functionality to a logger should be done using a more ruby approach such as `logger.extend(AS::TaggedLogging)`. Fix rails#49757 Fix rails#49745 Fix rails#46084 Fix rails#44668 I made a propose on the Ruby logger upstream in ruby/logger#90, but it hasn't been reviewed it.
- ### Context The Tagged Logging functionality has been a source of a few issues over the years, especially when combined with the broadcasting feature. Initializating a Tagged Logger wasn't very intuitive: ```ruby logger = Logger.new(STDOUT) tagged_logger = ActiveSupport::TaggedLogging.new(logger) # Would expect the `tagged_logger` to be an instance of `AS::TaggedLogging` # but it's a clone of the `logger`. tagged_logger.formatter = ->(_, _, _, message) do { message: message } end # Modifies the formatter to output JSON formatted logs. # This breaks tagged logging. ``` I believe the main reason of those issues is because tagged logging is implemented at the wrong level. ### Solution I made a proposal on the Ruby logger upstream in ruby/logger#90 to help solve this but it hasn't been reviewed yet. So I thought about adding it here for now. The TL;DR is to decouple formatting and adding extra information to logs (which is what tagged logging does). ### Deprecation Since TaggedLogging will no longer access the formatter, there is a few things I'd like to deprecate (such as setting a default formatter https://github.com/rails/rails/blob/d68e43922bc11829c52ad9f736ad5549fc97631b/activesupport/lib/active_support/tagged_logging.rb#L124) but doing so in this PR would increase the size of the diff significantly and would maybe distract for PR reviews. Another thing that I believe should be deprecated is `ActiveSupport::TaggedLogging.new`. Adding tagging functionality to a logger should be done using a more ruby approach such as `logger.extend(AS::TaggedLogging)`. Fix rails#49757 Fix rails#49745 Fix rails#46084 Fix rails#44668 I made a propose on the Ruby logger upstream in ruby/logger#90, but it hasn't been reviewed it.
I'm trying to create a tagged logger (
final_logger
) from another tagged logger (tagged_logger2
) that is receiving broadcasts from yet another tagged logger (tagged_logger1
). This situation triggers an odd behaviour: tags applied tofinal_logger
leak upstream to the originaltagged_logger1
.Steps to reproduce
To reproduce, run this test case (it can be found on my fork here):
Expected behavior
I would expect this test to pass.
Actual behavior
The assertions on the logs in
sink1
fail; they are prefixed with "[test]":System configuration
We originally thought this bug was related to this PR. It was reverted, but the problem persists. Our thinking now is that the PR re-implemented the bug in a different way, so reverting had no effect.
Rails version:
Ruby version:
3.1.2
The text was updated successfully, but these errors were encountered: