Skip to content

Clone to keep extended Logger methods for tagged logger#40759

Merged
rafaelfranca merged 1 commit intorails:masterfrom
orhantoy:broadcast-tagged-logging
Dec 8, 2020
Merged

Clone to keep extended Logger methods for tagged logger#40759
rafaelfranca merged 1 commit intorails:masterfrom
orhantoy:broadcast-tagged-logging

Conversation

@orhantoy
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

3 participants