-
Notifications
You must be signed in to change notification settings - Fork 757
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
how to get field value #3215
Comments
I had the same issue today and found a workaround by using a custom visitor that implements But I wonder if there is a good reason why there is no function like |
How to use it? |
Suppose you want to read the field value from an #[derive(Default)]
struct ExampleVisitor(pub Option<String>);
impl tracing::field::Visit for ExampleVisitor {
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
// Mandatory to implement
}
fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
if field.name() == "EXAMPLE" {
// Extract the field value if the name matches our searched field name
self.0 = Some(value.to_string());
}
}
}
struct MyLoggingLayer;
impl<S: tracing::Subscriber> tracing_subscriber::layer::Layer<S> for MyLoggingLayer {
fn on_event(&self, event: &tracing_core::event::Event<'_>, _ctx: tracing_subscriber::layer::Context<'_, S>) {
// Listen for logging events in this logging layer
// Visit the records/fields of the event with out custom visitor
let my_visitor = ExampleVisitor::default();
event.record(&mut my_visitor);
if let Some(field_val) = my_visitor.0 {
println!("Found {} at the field value EXAMPLE", field_val);
}
}
} The idea here is that the event allows you to pass a custom struct that implements I hope this helps you. |
I can get the field name by Field: 8000 :name method, but how to get field value?
The text was updated successfully, but these errors were encountered: