fix: loggers created with With() now inherit level changes on the parent#216
Open
troclaux wants to merge 1 commit into
Open
fix: loggers created with With() now inherit level changes on the parent#216troclaux wants to merge 1 commit into
troclaux wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #184.
Problem
With()creates a newLoggerby shallow-copying the struct (sl := *l). Becauselevelwas anint64value field, the copy held an independent integer. Any subsequentSetLevelcall on the parent had no effect on the child, and vice-versa.Fix
Change
levelfromint64to*int64.NewWithOptionsallocates a freshint64for every root logger.With()copies the pointer, so the parent and all children derived from it share the same memory cell. ASetLevelcall 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.