Skip to content

Commit

Permalink
Use normalized_metadata instead of manually normalizing metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
MaeIsBad committed Feb 29, 2024
1 parent 5990f83 commit 206f04d
Showing 1 changed file with 2 additions and 31 deletions.
33 changes: 2 additions & 31 deletions src/json/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::io::Write;
use serde::ser::{SerializeMap, Serializer};
use serde::Serialize;
use tracing::{Event, Metadata, Subscriber};
use tracing_log::NormalizeEvent;
use tracing_subscriber::{
fmt::MakeWriter,
layer::Context,
Expand Down Expand Up @@ -118,7 +119,7 @@ impl EventFormatter for DefaultEventFormatter {
where
S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
{
let metadata = event.metadata();
let metadata = event.normalized_metadata();
let mut buffer = Vec::new();
let mut serializer = serde_json::Serializer::new(&mut buffer);
let mut map_serializer = serializer.serialize_map(None)?;
Expand Down Expand Up @@ -209,36 +210,6 @@ where
let mut map_serializer = serializer.serialize_map(None)?;

map_serializer.serialize_entry("environment", self.environment)?;
map_serializer.serialize_entry(
"target",
self.visitor
.get("log.target")
.unwrap_or_else(|| self.metadata.target()),
)?;
map_serializer.serialize_entry(
"file",
self.metadata
.file()
.or_else(|| self.visitor.get("log.file"))
.unwrap_or("-"),
)?;
map_serializer.serialize_entry(
"line",
&self
.metadata
.line()
.or_else(|| self.visitor.get("log.line"))
.unwrap_or(0),
)?;

for (key, value) in self
.visitor
.fields()
.iter()
.filter(|(&key, _)| key != "message" && !key.starts_with("log."))
{
map_serializer.serialize_entry(key, value)?;
}

if let Some(current_span) = self
.ctx
Expand Down

0 comments on commit 206f04d

Please sign in to comment.