Use track_caller approach for logging from a method
#964
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.
I noticed that Zed uses the
track_callerapproach in the log methods of their extension trait forResult: https://github.com/zed-industries/zed/blob/a910c594d6c5f6a808aa3c45ca4f8e5bba66ae75/crates/util/src/util.rs#L554We've already used that
track_callerfeature to great effect in #955.They also have neat methods such as
debug_assertandanyhow.This PR removes the existing disparate infra we have for logging errors to use that new trait.
Compared to
log_error!:The methods have postfix syntax, which is often nicer especially when you don't want to obscure the main logic.
I was a bit confused when I stumbled upon this today:
This made it seem the logging was unconditional.
Compared to
ResultOrLog:log_err()method does not require adding context. I think that's helpful for things like.send()to a channel where you don't quite want to panic if not related to essential channels, but you don't want to add too much clutter/overhead to the error handling either. You can still add context if you want to with anyhow's.context()method.logdisplays file/line info after the error message, we manually reiterate the file/line info before the message so the emitter of the log message is immediately identifiable.Before with
ResultOrLog:After with
ResultExt: