Skip to content

Commit

Permalink
subscriber: use display_timestamp and display_level in `Json::for…
Browse files Browse the repository at this point in the history
…mat_event` (#1463)

## Motivation

It should be possible to remove `timestamp` and `level` fields when
using json formatting.

As of now this has no effect:

```rs
let subscriber = tracing_subscriber::fmt()
    .with_level(false)
    .without_time()
    .json()
    .finish();
```

## Solution

Use the existing `display_timestamp` and `display_level` fields to
conditionally serialize `timestamp` and `level`.
  • Loading branch information
lerouxrgd authored and hawkw committed Aug 17, 2021
1 parent 8568a30 commit 86ff5c0
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions tracing-subscriber/src/fmt/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,13 @@ where

let mut serializer = serializer.serialize_map(None)?;

serializer.serialize_entry("timestamp", &timestamp)?;
serializer.serialize_entry("level", &meta.level().as_serde())?;
if self.display_timestamp {
serializer.serialize_entry("timestamp", &timestamp)?;
}

if self.display_level {
serializer.serialize_entry("level", &meta.level().as_serde())?;
}

let format_field_marker: std::marker::PhantomData<N> = std::marker::PhantomData;

Expand Down

0 comments on commit 86ff5c0

Please sign in to comment.