Tracing crate: constructing dynamic fields for an event?

I've been using the tracing crate, and have a more complex problem than usual:

This project is a REST API, and I have a rather complex error structure set up. This type is already set up to generate a REST response on error, which may have a set of different fields on the response based on the kind of error.

I'd like to also start logging these errors as tracing events, with some of the same events. Rather than passing each possible field by name to the event! macro, is there a way to write the macro in such a way that it accepts a dynamic list of fields? e.g. instead of

event!(myerrortype.tracing_level(), error_kind=myerrortype.error_kind(), var_1=myerrortype.var_1(), message=myerrortype.message())

I wish I could delegate the fields to my error type, e.g.

impl MyErrorType {
  pub fn tracing_level(&self) -> tracing::Level {...}
  pub fn tracing_fields(&self) -> Vec<(String, String)> { ... }

fn rest_error_handler(...) {
  event!(myerror.tracing_level(). *myerror.tracing_fields());

But having read through the macro source for the tracing crate, i don't see a way to do this, and I'm not sure how to do it "by hand" since the event! macro does so much.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.