-
Notifications
You must be signed in to change notification settings - Fork 301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Add capability to configure loggers inline #243
Draft
Mordil
wants to merge
1
commit into
apple:main
Choose a base branch
from
Mordil:nh-logger-metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains 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
Can one of the admins verify this patch? |
Mordil
changed the title
Add capability to configure loggers inline
RFC: Add capability to configure loggers inline
Dec 22, 2022
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) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm definitely expecting some bikeshedding on the names & labels of all of these - hence the lack of symbol docs
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
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