Description
Feature Request
My request is that tracing::instrument
supports using the pretty-print debug specifier (i.e., {:#?}
) to print fields instead of the regular debug specifier (i.e.,{:?}
).
Related to / possibly overlapping with:
- Can
#[instrument]
macro offer default format specifier #1312 - Introduce a formatter sigil for "alternate"
Display
#1311 - Add ability to print error
Result
usingerr
viaDebug
instead of onlyDisplay
#1630 - Alternative field wrappers in
tracing-attributes
#1089
Crates
tracing-attributes
Motivation
I use tracing::instrument
liberally when hacking on the Rust compiler. Oftentimes, the logs get really hard to read when a function has many parameters and each parameter has many fields and nested structs. I very frequently find myself wanting the ability to pretty-print fields so I can more easily see what's going on.
Proposal
No strong feelings on exactly how it should work. Here's one idea:
Two new options, pretty
and pretty_all
:
#[tracing::instrument(skip(self), pretty(bing, bang))]
fn foo(self, bing: BigNestedStruct, bong: u32, bang: BigNestedStruct) {}
#[tracing::instrument(pretty_all)]
fn bar(sneeb: BigNestedStruct, snob: BigNestedStruct) {}
Perhaps this could be generalized so that users could write their own field formatter and easily inject that into the instrument
invocation:
#[tracing::instrument(fmt = tracing::field::debug)] // default behavior
fn foo(bing: &str, bong: u32) {}
#[tracing::instrument(fmt = path::to::my_formatter)]
fn bar(sneeb: BigNestedStruct, snob: BigNestedStruct) {}
EDIT: I think this is precisely the idea of #1312
Alternatives
Quoting from #1089 (comment):
Note, though, that you can also do this by skipping all the automatically-generated fields, and adding custom fields using your own wrapper type. For example:
#[tracing::instrument( skip(foo, bar), fields(foo = ?my_wrapper(foo), bar = ?my_wrapper(bar)) )] fn my_function(foo: Foo, bar: Bar) { /// ... }
Too much work :P