Skip to content

Commit 55858d2

Browse files
authored
Fix console logging with values (#108)
1 parent f8daad1 commit 55858d2

File tree

2 files changed

+58
-19
lines changed

2 files changed

+58
-19
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "logfire"
3-
version = "0.8.1"
3+
version = "0.8.2"
44
edition = "2024"
55
license = "MIT"
66
rust-version = "1.85"

src/internal/exporters/console.rs

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ impl ConsoleWriter {
207207

208208
fn with_writer<R>(&self, f: impl FnOnce(&mut dyn Write) -> R) -> R {
209209
match &self.options.target {
210-
Target::Stdout => f(&mut io::stdout()),
211-
Target::Stderr => f(&mut io::stderr()),
210+
Target::Stdout => f(&mut io::stdout().lock()),
211+
Target::Stderr => f(&mut io::stderr().lock()),
212212
Target::Pipe(p) => f(&mut *p.lock().expect("pipe lock poisoned")),
213213
}
214214
}
@@ -362,20 +362,10 @@ impl ConsoleWriter {
362362
if !fields.is_empty() {
363363
for (idx, (key, value)) in fields.iter().enumerate() {
364364
let key = key.as_str();
365-
let value_str = match value {
366-
opentelemetry::logs::AnyValue::String(s) => s.as_str(),
367-
opentelemetry::logs::AnyValue::Int(i) => {
368-
return write!(w, " {}={}", ITALIC.paint(key), i);
369-
}
370-
opentelemetry::logs::AnyValue::Double(d) => {
371-
return write!(w, " {}={}", ITALIC.paint(key), d);
372-
}
373-
opentelemetry::logs::AnyValue::Boolean(b) => {
374-
return write!(w, " {}={}", ITALIC.paint(key), b);
375-
}
376-
_ => &format!("{value:?}"),
377-
};
378-
write!(w, " {}={}", ITALIC.paint(key), value_str)?;
365+
366+
write!(w, " {}=", ITALIC.paint(key))?;
367+
write_any_value(w, value)?;
368+
379369
if idx < fields.len() - 1 {
380370
write!(w, ",")?;
381371
}
@@ -386,6 +376,53 @@ impl ConsoleWriter {
386376
}
387377
}
388378

379+
fn write_any_value<W: io::Write>(w: &mut W, value: &AnyValue) -> io::Result<()> {
380+
match value {
381+
AnyValue::Int(i) => {
382+
write!(w, "{i}")?;
383+
}
384+
AnyValue::Double(d) => {
385+
write!(w, "{d}")?;
386+
}
387+
AnyValue::String(s) => {
388+
write!(w, "{s}")?;
389+
}
390+
AnyValue::Boolean(b) => {
391+
write!(w, "{b}")?;
392+
}
393+
AnyValue::Bytes(items) => {
394+
write!(w, "{}", DIMMED.paint(format!("<bytes:{}>", items.len())))?;
395+
}
396+
AnyValue::ListAny(list_values) => {
397+
for (idx, val) in list_values.iter().enumerate() {
398+
write_any_value(w, val)?;
399+
400+
if idx < list_values.len() - 1 {
401+
write!(w, ",")?;
402+
}
403+
}
404+
}
405+
AnyValue::Map(hash_map) => {
406+
write!(w, "{{")?;
407+
408+
for (idx, (key, val)) in (**hash_map).iter().enumerate() {
409+
write!(w, "{}=", ITALIC.paint(key.as_str()))?;
410+
write_any_value(w, val)?;
411+
412+
if idx < hash_map.len() - 1 {
413+
write!(w, ",")?;
414+
}
415+
}
416+
write!(w, "}}")?;
417+
}
418+
other => {
419+
write!(w, "{other:?}")?;
420+
}
421+
}
422+
423+
Ok(())
424+
}
425+
389426
#[cfg(test)]
390427
mod tests {
391428
use std::sync::{Arc, Mutex};
@@ -422,6 +459,7 @@ mod tests {
422459
let _ = crate::span!(level: Level::DEBUG, "debug span");
423460
let _ =
424461
crate::span!(parent: &root, level: Level::DEBUG, "debug span with explicit parent");
462+
crate::info!("log with values", foo = 42, bar = 33);
425463
crate::info!("hello world log");
426464
panic!("oh no!");
427465
}))
@@ -438,8 +476,9 @@ mod tests {
438476
1970-01-01T00:00:00.000001Z INFO logfire::internal::exporters::console::tests hello world span
439477
1970-01-01T00:00:00.000002Z DEBUG logfire::internal::exporters::console::tests debug span
440478
1970-01-01T00:00:00.000003Z DEBUG logfire::internal::exporters::console::tests debug span with explicit parent
441-
1970-01-01T00:00:00.000004Z INFO logfire::internal::exporters::console::tests hello world log
442-
1970-01-01T00:00:00.000005Z ERROR panic: oh no! backtrace=disabled backtrace
479+
1970-01-01T00:00:00.000004Z INFO logfire::internal::exporters::console::tests log with values foo=42, bar=33
480+
1970-01-01T00:00:00.000005Z INFO logfire::internal::exporters::console::tests hello world log
481+
1970-01-01T00:00:00.000006Z ERROR panic: oh no! backtrace=disabled backtrace
443482
");
444483
}
445484

0 commit comments

Comments
 (0)