home ~ projects ~ socials

Pretty Print Rust tracing Log Output

This is how I'm pretty printing multi-line log messages in Rust with the tracing and tracing-subscriber crates

```cargo
[dependencies]
tracing = "0.1"
tracing-subscriber = "0.3"
```

use tracing::{Level, event, instrument};

#[instrument]
fn main() {
    let format = tracing_subscriber::fmt::format().pretty();
    tracing_subscriber::fmt()
      .event_format(format)
      .with_ansi(false)
      .with_max_level(Level::DEBUG)
      .init();
  event!(Level::INFO, "This is an info message");
  event!(Level::DEBUG, "This is a debug message");
}
Output:
2024-04-23T14:39:34.254964Z  INFO _active_nvim_run: This is an info message
    at /Users/alan/.cargo/target/55/19854259915251/_active_nvim_run:17

  2024-04-23T14:39:34.255026Z DEBUG _active_nvim_run: This is a debug message
    at /Users/alan/.cargo/target/55/19854259915251/_active_nvim_run:18
-- end of line --

References