Repository navigation
feat(logging): add a setting to panic when add_fields! overwrites a key - #286
Open
LubabaKhalid wants to merge 1 commit into
Open
LubabaKhalid wants to merge 1 commit into
LubabaKhalid wants to merge 1 commit into
Conversation
Adding the same key with `add_fields!` again and again without forking the logger builds a long logger chain, which can overflow the stack when it is dropped. Add `LoggingSettings::panic_on_field_overwrite`. When enabled, `add_fields!` panics if it sets a key that the current logger already has, including keys inherited from a parent logger. Intentional overwrites opt out with `add_fields!(allow_overwrite; ...)`. The flag is stored on the logger, like the frozen state, so forked loggers inherit it and test contexts can enable it through `set_logging_settings`.
Author
|
@TheJokr this adds the setting to panic when a log field is overwritten, with an |
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.
Calling
add_fields!in a loop without forking the logger adds one level to the logger chain on every call, and a long enough chain overflows the stack when it is dropped. The nesting limit and logger freezing already catch some of these cases, but often the cause is the same key being set over and over.This adds
LoggingSettings::panic_on_field_overwrite(off by default). When it is enabled,add_fields!panics if it sets a key that the current logger already has, including keys inherited from a parent logger. The panic message names the key. Intentional overwrites can opt out per call:The flag is stored on the logger next to the frozen state, so forked loggers inherit it and test contexts can turn it on with
set_logging_settings. The check walks the keys of the logger's existing fields on eachadd_fields!call, so it is meant as a debugging aid rather than something to leave on in hot paths.The internal
add_log_fieldsfunction used by the macro takes an extraallow_overwriteargument. It lives in the#[doc(hidden)]internal module, so this does not change the public API.Testing
allow_overwriteletting the overwrite through.cargo test -p foundations --test loggingand theadd_fields!doctest.foundationstests pass. The panic hook and telemetry init tests rely on process-global state, so I ran those one per process, the way nextest runs them in CI.cargo clippy -p foundations --all-targetswith the CI lint flags,cargo checkwithlogging,logging,settingsandtelemetryalone, andrustfmt --check --edition 2024.Closes #243