-
Notifications
You must be signed in to change notification settings - Fork 256
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
Consider bringing back kv
Value
downcasting
#641
Comments
Thanks for the note @AlexTMjugador 👍 We did a lot of discussion back and forth in PRs while pruning and stabilizing these APIs, so it probably has all ended up buried. Downcasting support was soft-deprecated in I'd be happy to revisit stabilizing downcasting. |
Thanks for the reply @KodrAus, it's very appreciated! I can understand how downcasting can indeed be brittle when considering the broader crate ecosystem, as Rust’s usual type-safety expectations could be undermined if duck typing in log record values gets overused across crates maintained by different people. If this sort of brittleness was the main concern for not stabilizing this feature, perhaps a clear warning in the docs advising crate authors to limit it to applications that control both logging and logger components or opportunistic use cases would be sufficient to mitigate any wider issues? |
While working on a Rust application, I became interested in downcasting a
Value
associated with a log record when using thekv
feature of thelog
crate. To my surprise, I discovered that this capability was removed when thekv
feature was stabilized:log/src/kv/value.rs
Lines 1100 to 1107 in 96dbf58
In my view, there are compelling use cases for downcasting log record
Value
s: that enables different software components to process them in a loosely coupled way, effectively repurposing logRecord
s as a versatile, typed message-passing mechanism.For instance, imagine integrating an existing codebase that logs
anyhow::Error
s withsentry
reporting, with the requirement that generated Sentry events should include the error backtrace. WithValue
downcasting, this could be achieved by doing this at the logging site:And then, to emit Sentry events from these log records with the error backtrace, one could simply pass a custom mapper function to a
SentryLogger
that retrieves theerror
value from the record, downcasts it to ananyhow::Error
, calls thebacktrace()
method on it, and attaches that backtrace to the Sentry event.Without record
Value
downcasting, there isn’t an elegant solution for the scenario above. Approaches like using customserde
serializers or establishing an additional communication channel between log sites and logger implementations may work in certain cases, but they are far from ideal.On a different codebase, but this time proof of concept and open-source, I've also found downcasting useful for passing along typed status update messages that get formatted in the way most appropriate for the target environment by just switching between custom logger implementations.
I've scoured the repository for any issues or commits explaining the rationale for removing
Value
downcasting but couldn’t find anything beyond a suggestion to file an issue if this is a feature "you rely on". Therefore, I’m opening this issue to understand the context of its removal and to inquire if adding it back is feasible. Are there any drawbacks or implementation constraints I might be overlooking?Edit: after giving it more thought, the Sentry integration example above perhaps could be solved by using the
downcast
method available ondyn Error
, asanyhow::Error
s implement theError
trait... But that's still not helpful for the different codebase example, and not applicable when introspectingValue
s other thanError
s is desired.Edit 2: nevermind the edit above,
anyhow::Error
does not implementstd::error::Error
and thus cannot be downcasted like that.The text was updated successfully, but these errors were encountered: