Skip to content

Clear level overrides in #dup and #clone - #137

Open
nevans wants to merge 2 commits into
ruby:masterfrom
nevans:clone-resets-logger_override
Open

Clear level overrides in #dup and #clone#137
nevans wants to merge 2 commits into
ruby:masterfrom
nevans:clone-resets-logger_override

Conversation

@nevans

@nevans nevans commented Oct 1, 2025

Copy link
Copy Markdown

Cloning loggers can be used, for example, to create different loggers for different classes/components/subsystems that share the same log_dev and other configuration (possibly a subclass of Logger), but with different verbosity levels, formatters, etc.

But, that can break if they share @level_override. Rather than use OverrideMap.new, the existing @level_override is cloned and cleared, to preserve @level_override's class.


This PR is based on #136 to avoid merge conflicts. But it can be updated to be independent of that other PR, if so desired.

nevans added 2 commits October 1, 2025 12:16
This allows fiber keys to be GCed and removed from the map.  Otherwise,
fibers that call `#with_level` create a memory leak if they are killed
without running their ensure blocks.
Cloning loggers can be used, for example, to create different loggers
for different classes/components/subsystems that share the same log_dev
and other configuration (possibly a subclass of Logger), but with
different verbosity levels, formatters, etc.

But, that doesn't work if they share `@level_override`.  Rather than
use OverrideMap.new, the existing `@level_override` is cloned and
cleared, to preserve `@level_override`'s class.
@nevans
nevans force-pushed the clone-resets-logger_override branch from 5f37f2b to 1b4e472 Compare October 1, 2025 17:05
@OskarEichler

Copy link
Copy Markdown

I re-verified this copy-isolation change against Logger 1.7.0/current and found one adjacent internal object that remains shared: @default_formatter. Mutating the copy's default formatter configuration can therefore affect the original, while explicit custom formatters should continue to be shared by caller choice.

I prepared a one-line follow-up directly on this PR head: OskarEichler@08d2055

It duplicates only Logger's internal default formatter inside the existing initialize_copy hook. The release/current suites pass on Ruby 4.0.6 and 3.2.11, and the focused copy model verifies isolated internal formatter state while explicit custom formatter identity remains shared. No repository tests were changed. I kept this on #137's lineage to preserve the original ownership point and avoid a competing PR.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants