Skip to content

Support pretty-printing fields tracing::instrument (i.e., {:#?} instead of {:?}). #1752

Open
@BGR360

Description

@BGR360

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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions