Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

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

Code

```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");
}

Results

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

References