RFC: Add capability to configure loggers inline #243
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.
Adds initializers and methods to create Logger instances with metadata inline.
Motivation:
Frequently it's needed to create copies of loggers, or a new logger, with attached metadata or specific log levels, but the current method requires first creating an instance, as a
var
, and then configuring the instance.This leaves you to have a mutable value existing in the scope even if you don't want to.
This particularly has issues with async code when capturing the instance in a
Task
because it's non-isolatedThe best way to get around this is either
Modifications:
makeCopy(with:mergePolicy:)
to allow just setting inline the metadata you want the new instance to have, with control on how to merge with existing metadata keysmakeCopy(_:)
which passes aninout
reference to the new instance, allowing you to set more options such aslogLevel
as part of creationinit(label:metadata:)
that allows creation of instances directly inline that directly sets the metadatainit(label:_:)
that allows creation of instances with a reference to the object for inline configuration much likemakeCopy(_:)
Result:
Developers will now be able to make
Logger
instances, or "children" copies, directly inline with full control over the value's mutability - making it safe to send acrossTask
boundaries