tracing icon indicating copy to clipboard operation
tracing copied to clipboard

Possibility to override default fields name with JSON formatter

Open jaudiger opened this issue 3 years ago • 2 comments

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.

jaudiger avatar Jan 21 '22 16:01 jaudiger

Greetings!

@hawkw Any update on this? Faced with same problem and implementing own Json-Layer is super counterproductive.

BratSinot avatar May 25 '22 12:05 BratSinot

@BratSinot I don't think anyone is actively working on this, but we'd be glad to accept PRs

bryangarza avatar May 25 '22 15:05 bryangarza