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.

Write A Log File With The tracing Rust Crate

This is how I'm writing out a log file in Rust. It removes the file each time at the start of the process. Not always what I want, but super helpful in lots of cases. If I want to keep the logs going over time, I use

Code

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

use tracing::{Level, event, instrument};
use std::path::PathBuf;
use std::fs;

#[instrument]
fn main() {
  let log_file = PathBuf::from("./example3.log");
  let _ = fs::remove_file(&log_file);
  let file_appender = tracing_appender::rolling::never(
    log_file.parent().unwrap(), 
    log_file.file_name().unwrap()
  );
  let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
  tracing_subscriber::fmt()
    .with_ansi(false)
    .with_writer(non_blocking)
    .init();
  event!(Level::INFO, r#"This is the log message"#);
}

Code

tracing_appender::rolling::hourly()

instead of

Code

tracing_appender::rolling::never()