RFC: Add capability to configure loggers inline#243
Draft
Mordil wants to merge 1 commit intoapple:mainfrom
Draft
RFC: Add capability to configure loggers inline#243Mordil wants to merge 1 commit intoapple:mainfrom
Mordil wants to merge 1 commit intoapple:mainfrom
Conversation
|
Can one of the admins verify this patch? |
Mordil
commented
Dec 22, 2022
Comment on lines
+670
to
+697
| public func makeCopy(with metadata: Metadata, mergePolicy: Metadata.MergingPolicy = .retainExistingValues) -> Logger { | ||
| let originalMetadata = self.handler.metadata | ||
| var child = self | ||
| child.handler.metadata = originalMetadata.merging(metadata, uniquingKeysWith: { old, new in | ||
| switch mergePolicy.policy { | ||
| case .replaceValues: return new | ||
| case .retainExistingValues: return old | ||
| } | ||
| }) | ||
| return child | ||
| } | ||
|
|
||
| @inlinable | ||
| public func makeCopy(_ configure: (inout Logger) -> Void) -> Logger { | ||
| var child = self | ||
| configure(&child) | ||
| return child | ||
| } | ||
|
|
||
| public init(label: String, metadata: Metadata) { | ||
| self.init(label: label) | ||
| self.handler.metadata = metadata | ||
| } | ||
|
|
||
| public init(label: String, _ configure: (inout Logger) -> Void) { | ||
| self.init(label: label) | ||
| configure(&self) | ||
| } |
Author
There was a problem hiding this comment.
I'm definitely expecting some bikeshedding on the names & labels of all of these - hence the lack of symbol docs
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.
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
Taskbecause 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 aninoutreference to the new instance, allowing you to set more options such aslogLevelas 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
Loggerinstances, or "children" copies, directly inline with full control over the value's mutability - making it safe to send acrossTaskboundaries