feat!: add ContextPair to StructuredLogger #903
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.
Motivation
addContext
has one sharp edge on it that seems to come up a lot during onboarding: if you want to add a single context element, the syntax is unintuitive.Say we want to add the context
"foo" -> "bar"
to a logger. Naturally, one might reach foraddContext(pairs: (String, Shown)*)
, but alas, after erasure we havewhich doesn't conform to either of
Map
orSeq
. This leaves us with these options:Neither feels great, and it would be really pleasant to have consistent syntax for single-element and multi-element context data without having to use
Map
literals and explicit.show
/.toString
.Result
This patch hijacks
StructuredLogger
's implicit scope to do a bit of juggling and resolve the erasure issue, and in the process cleans up a chunk of duplicative code in its descendants.I've tested this in the work code, and it appears to work as expected for any subtype of
StructuredLogger
. The signature ain't exactly beautiful, but the syntactic consistency is gratifying.I'll add tests if this looks tolerable, just wanted to get the idea out of my head.