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

Clone to keep extended Logger methods for tagged logger #40759

Merged
merged 1 commit into from Dec 8, 2020
Merged

Clone to keep extended Logger methods for tagged logger #40759

merged 1 commit into from Dec 8, 2020

Conversation

orhantoy
Copy link
Contributor

@orhantoy orhantoy commented Dec 7, 2020

Summary

#dup resets the extended Logger methods that could come from enabling broadcasting. That would mean if we create a tagged logger from a Logger with broadcasting enabled (usually to stdout), the new tagged logger will not perform broadcasting.

Other Information

I'm not sure if the current behavior is as expected, but it seemed surprising to me. If your Logger has broadcasting enabled, it feels like the derived tagged loggers should also broadcast. Happy to hear what you think.

Relates to #27792 and #38850

`#dup` resets the extended Logger methods that could come from enabling broadcasting. That would mean if we create a tagged logger from a Logger with broadcasting enabled (usually to stdout), the new tagged logger will not perform broadcasting.
@rafaelfranca rafaelfranca merged commit eebde10 into rails:master Dec 8, 2020
rafaelfranca added a commit that referenced this pull request Sep 22, 2021
Clone to keep extended Logger methods for tagged logger
@jdelStrother
Copy link
Contributor

jdelStrother commented Mar 11, 2022

This does sound like a sensible change, though it does break broadcasting to multiple loggers with different formatters. In Rails 6 this used to work:

logger = ActiveSupport::TaggedLogging.new(Logger.new "base.log")
logger2 = Logger.new("fancy.log"); logger2.formatter = proc{|_,_,_,msg| "*** #{msg} ***" }
logger.extend(ActiveSupport::Logger.broadcast(logger2))

logger.info "Hello"

this would write "Hello" to base.log, and "*** Hello ***" to fancy.log.

In Rails 7, if you call logger.tagged("ABC"), it resets the log formatter on logger2, so it would just write "Hello" to both logs.
Should this use case be supported?

@rafaelfranca
Copy link
Member

There is no reason to break that behavior. Can you try to fix and open a PR? It seems we just need to copy the formatter from the original logger.

@jdelStrother
Copy link
Contributor

@rafaelfranca will do.
The behaviour in Logger.broadcast where setting log_formatter on one logger will propagate it to all broadcasted loggers seems a very intentional choice, but I don’t really get the use case. Any idea?

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

Successfully merging this pull request may close these issues.

None yet

3 participants