Skip to content

Possibility to override default fields name with JSON formatter #1849

@jaudiger

Description

@jaudiger

Feature Request

Crates

tracing-subscriber

Motivation

I wanted to update the fields name of the JSON subscriber to mimic the ones produced by other internal services. At the end, all the logs are retrieved by fluent bit, and I wanted to simplify the aggregation of our system logs.

If I'm doing the solution proposed below, it has currently no impact.

Proposal

  let formatter = format::debug_fn(|writer, field, value| {
      match field.name() {
          "target" => write!(writer, "my_field:  {:?}", value),
          _ => write!(writer, "{}:  {:?}", field, value)
      }
  })
  .delimited(", ");

  let subscriber = tracing_subscriber::fmt()
      .with_max_level(Level::INFO)
      .json()
      .flatten_event(true)
      .fmt_fields(formatter)
      .finish();

  tracing::subscriber::set_global_default(subscriber)
      .with_context(|| "Failed to initialize the logging library")?;

Alternatives

I could reimplement the serializer to customize it as I would like to be, but that's not the best solution. And it can be hard to maintain across tracing releases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    crate/subscriberRelated to the `tracing-subscriber` cratekind/featureNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions