Skip to content

Can #[instrument] macro offer default format specifier #1312

@bobyangyf

Description

@bobyangyf

Feature Request:

Crates

tracing_attributes

Motivation

Currently, instrument macro uses debug format for all arguments on the function annotated.
However, sometimes the display version of the arguments is preferred as it's often more human friendly. The only current way to do so is to manually add each one to the fields argument with the display format specified. This is verbose, and error prone when modifying the function arguments.
e.g.

#[instrument(fields(a = %a, b = %b, c = %c, d = %d, ...)]
fn foo(a: Box<dyn Display>, b: Box<dyn Display>, ...) { ... }

Proposal

Add a new argument to instrument macro that allows specifies the format of the function arguments.
Example

#[instrument(fmt = tracing::value::display)
fn foo(a: Box<dyn Display>, b: Box<dyn Display>, ...) { ... }

We can change this line. https://docs.rs/tracing-attributes/0.1.15/src/tracing_attributes/lib.rs.html#652
to not hard-code debug format.

Open to implementing this change if there's no objections to this feature.

Alternatives

  1. Leave as is. => :(
  2. introduce a separate macro like display_instrument

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