-
Notifications
You must be signed in to change notification settings - Fork 333
[SLG-0003]: Standardized Error Metadata via Logger Convenience #405
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
base: main
Are you sure you want to change the base?
Conversation
|
I have some thoughts:
|
| } | ||
| ``` | ||
|
|
||
| This was rejected due to breaking the existing abstraction layer between `Logger` and `LogHandler`, while also being questionable how much value it would add. No newline at end of file |
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.
It would be great if LogHandler could accept Error objects directly. That would let us forward non-fatal errors to Crashlytics (or similar services) and improve monitoring in apps.
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 agree, however that's out of scope for this proposal. That was my original idea in #384, but as per suggested in #384 (comment) we split it in two phases to help moving forward. That is, if/when this is implemented, I might add a follow-up proposal, though from the discussion in my previous PR it’s not obvious if that’s a direction swift-log wants to go in.
|
|
||
| ### Detailed design | ||
|
|
||
| The proposed solution is to add a new overload for `Logger.log`, that accepts an `Error` as its third argument and serializes it, adding the type and message to the `metadata` parameter. |
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 think we should also add to this proposal a way to set the error metadata on a Logger:
extension Logger {
public mutating func recordError(_ error: any Error) {
self[metadataKey: "error.type"] = "\(String(reflecting: type(of: error)))"
self[metadataKey: "error.message"] = "\(error)"
}
}This means the user can do any of the following:
let error: any Error = ...
let logger: Logger = ...
// Option 1:
logger.info("Stuff went wrong, error: error)
// Option 2:
logger.recordError(error)
logger.info("Stuff went wrong")We generally want to allow adopters to attach metadata on the logger as well, to make it easier to pass loggers with appropriate metadata to be passed into nested functions.
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.
Should I add it already at this stage, or would that be part of the review?
kukushechkin
left a comment
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.
Thank you for taking time to prepare the proposal! I have left a few suggestions and a few questions to discuss before starting the formal review process.
| Additionally, we add a new overload for each log method (`trace`, ..., `critical`) that accepts an `Error` as its second argument, that in turn calls the new `log` method. | ||
|
|
||
| ```swift | ||
| public func log( |
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.
We usually put all the public API changes in the Detailed Design section, as they would be in the code with the accompanying doc strings.
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 not sure what this comment suggests - these are in Detailed design. Did you mean to say Proposed solution?
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.
Oh, sorry for being unclear. I suggest adding the new methods and their docc comments, as this is also part of the "public interface" of the package.
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 see, added it now! Takes quite a bit of space with all the different methods 😅
Please, keep them as separate methods. Changing the method signature is strictly speaking an API breakage, even if default value keeps code compilable.
|
Add docc, link to custom implementation, fix wording feedback
@kukushechkin thanks for all your suggestions, really appreciate the help! |
| ```swift | ||
| extension Logger { | ||
| /// Metadata keys with specific use-cases | ||
| public enum WellKnownMetadataKey: String { |
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.
There is a good chance this enum will be extended with more keys in the future, so it should be up for backward compatible evolution. One option is @nonexhaustive, but this limits the supported Swift versions. Or a struct with static properties similar to this. I would lean towards the second option as LogHandler would not need to match over all the special keys supported by the Logger, but would rather reference a few interesting ones.
What do you think @czechboy0?
Add standardized way of attaching data from Error instances to log posts.
Motivation:
Addresses #291
Modifications:
The "SLG-0003" proposal doc added.
Result:
The proposal is ready for review.