From 86ff5c09d3424df5ab5fd4f704893d4b5d31c5ff Mon Sep 17 00:00:00 2001 From: Romain Leroux Date: Mon, 12 Jul 2021 20:53:28 +0200 Subject: [PATCH] subscriber: use `display_timestamp` and `display_level` in `Json::format_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`. --- tracing-subscriber/src/fmt/format/json.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tracing-subscriber/src/fmt/format/json.rs b/tracing-subscriber/src/fmt/format/json.rs index 8144a26661..2c86504f00 100644 --- a/tracing-subscriber/src/fmt/format/json.rs +++ b/tracing-subscriber/src/fmt/format/json.rs @@ -207,8 +207,13 @@ where let mut serializer = serializer.serialize_map(None)?; - serializer.serialize_entry("timestamp", ×tamp)?; - serializer.serialize_entry("level", &meta.level().as_serde())?; + if self.display_timestamp { + serializer.serialize_entry("timestamp", ×tamp)?; + } + + if self.display_level { + serializer.serialize_entry("level", &meta.level().as_serde())?; + } let format_field_marker: std::marker::PhantomData = std::marker::PhantomData;