Skip to content

fix: loggers created with With() now inherit level changes on the parent#216

Open
troclaux wants to merge 1 commit into
charmbracelet:mainfrom
troclaux:fix/with-inherits-level
Open

fix: loggers created with With() now inherit level changes on the parent#216
troclaux wants to merge 1 commit into
charmbracelet:mainfrom
troclaux:fix/with-inherits-level

Conversation

@troclaux

@troclaux troclaux commented Jun 2, 2026

Copy link
Copy Markdown

Fixes #184.

Problem

With() creates a new Logger by shallow-copying the struct (sl := *l). Because level was an int64 value field, the copy held an independent integer. Any subsequent SetLevel call on the parent had no effect on the child, and vice-versa.

Fix

Change level from int64 to *int64. NewWithOptions allocates a fresh int64 for every root logger. With() copies the pointer, so the parent and all children derived from it share the same memory cell. A SetLevel call on any of them is immediately visible to all.

All atomic operations are updated to dereference the pointer (atomic.LoadInt64(l.level) / atomic.StoreInt64(l.level, v)), preserving thread-safety.

With() copies the Logger struct by value, which gave the wrapped logger its
own independent int64 for the log level. Subsequent SetLevel calls on the
parent had no effect on any wrapped child.

Fix: change the level field to *int64. New loggers allocate a fresh int64;
With() copies the pointer, so parent and all children share the same memory
cell. SetLevel anywhere in the hierarchy is visible everywhere.

Fixes charmbracelet#184
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