Skip to content

Commit

Permalink
review second round - debug impl output changed to [de ad be ef] and …
Browse files Browse the repository at this point in the history
…json has its own implementation
  • Loading branch information
mladedav committed May 3, 2024
1 parent 7d935e8 commit 6ac7c0a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
16 changes: 12 additions & 4 deletions tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
use crate::callsite;
use core::{
borrow::Borrow,
fmt,
fmt::{self, Write},
hash::{Hash, Hasher},
num,
ops::Range,
Expand Down Expand Up @@ -292,11 +292,19 @@ struct HexBytes<'a>(&'a [u8]);

impl<'a> fmt::Debug for HexBytes<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for byte in self.0 {
f.write_char('[')?;

let mut bytes = self.0.iter();

if let Some(byte) = bytes.next() {
f.write_fmt(format_args!("{byte:02x}"))?;
}

Ok(())
for byte in bytes {
f.write_fmt(format_args!(" {byte:02x}"))?;
}

f.write_char(']')
}
}

Expand Down Expand Up @@ -1173,6 +1181,6 @@ mod test {
use core::fmt::Write;
write!(&mut result, "{:?}", value).unwrap();
});
assert_eq!(result, format!("{}", r#"616263" "c0ffee"#));
assert_eq!(result, format!("{}", r#"[61 62 63]" "[c0 ff ee]"#));
}
}
15 changes: 13 additions & 2 deletions tracing-subscriber/src/fmt/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,11 @@ impl<'a> field::Visit for JsonVisitor<'a> {
.insert(field.name(), serde_json::Value::from(value));
}

fn record_bytes(&mut self, field: &Field, value: &[u8]) {
self.values
.insert(field.name(), serde_json::Value::from(value));
}

fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
match field.name() {
// Skip fields that are actually log metadata that have already been handled
Expand Down Expand Up @@ -528,13 +533,19 @@ mod test {
#[test]
fn json() {
let expected =
"{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":42,\"name\":\"json_span\",\"number\":3,\"slice\":\"616263\"},\"spans\":[{\"answer\":42,\"name\":\"json_span\",\"number\":3,\"slice\":\"616263\"}],\"target\":\"tracing_subscriber::fmt::format::json::test\",\"fields\":{\"message\":\"some json test\"}}\n";
"{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":42,\"name\":\"json_span\",\"number\":3,\"slice\":[97,98,99]},\"spans\":[{\"answer\":42,\"name\":\"json_span\",\"number\":3,\"slice\":[97,98,99]}],\"target\":\"tracing_subscriber::fmt::format::json::test\",\"fields\":{\"message\":\"some json test\"}}\n";
let collector = collector()
.flatten_event(false)
.with_current_span(true)
.with_span_list(true);
test_json(expected, collector, || {
let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3, slice = &b"abc"[..]);
let span = tracing::span!(
tracing::Level::INFO,
"json_span",
answer = 42,
number = 3,
slice = &b"abc"[..]
);
let _guard = span.enter();
tracing::info!("some json test");
});
Expand Down

0 comments on commit 6ac7c0a

Please sign in to comment.