Skip to content

fix: inherit log level changes in wrapped subloggers - #230

Open
AdamMagued wants to merge 1 commit into
charmbracelet:mainfrom
AdamMagued:fix-issue-184-wrapped-logger-level
Open

AdamMagued wants to merge 1 commit into
charmbracelet:mainfrom
AdamMagued:fix-issue-184-wrapped-logger-level

Conversation

@AdamMagued

Copy link
Copy Markdown

Fixes #184

Problem

When creating a sublogger via With, the logger struct was shallow-copied by value. This decoupled the sublogger's level field from the parent logger. Calling SetLevel on the parent logger after sublogger creation did not update the sublogger's active log level.

Solution

  • Track parent-child logger relationships by adding a parent reference and a hasCustomLevel flag to Logger.
  • Update level checks (Log, GetLevel, and Enabled) to resolve the effective level by traversing the parent chain until reaching an explicitly configured level.
  • Set hasCustomLevel upon calling SetLevel, allowing subloggers to override their inherited level without mutating the parent or other siblings.
  • Added comprehensive unit tests validating level inheritance, explicit child overrides, multi-level hierarchies, and concurrency safety.

Subloggers created via With previously copied the parent logger struct by value, isolating their log level state from subsequent parent SetLevel calls. This change links subloggers to their parent hierarchy and resolves the active log level dynamically unless explicitly overridden on the sublogger.
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.

Wrapped loggers don't inherit level changes

1 participant