Skip to content

Instantly share code, notes, and snippets.

@mxxntype
Last active January 25, 2025 18:51
Show Gist options
  • Select an option

  • Save mxxntype/a1bbebbb7d7efcc04c23ba0db1b73a61 to your computer and use it in GitHub Desktop.

Select an option

Save mxxntype/a1bbebbb7d7efcc04c23ba0db1b73a61 to your computer and use it in GitHub Desktop.
A snippet of using eyre::Report
use color_eyre::eyre::Report;
fn main() -> Result<(), Report> {
color_eyre::install()?;
install_tracing()?;
let int = 512;
let string = "sample";
tracing::info!(?int, ?string, "Sample log message");
Ok(())
}
fn install_tracing() -> Result<(), Report> {
use tracing_error::ErrorLayer;
use tracing_subscriber::prelude::*;
use tracing_subscriber::{fmt, EnvFilter};
let filter_layer = EnvFilter::try_from_default_env().or_else(|_| EnvFilter::try_new("info"))?;
let format_layer = fmt::layer()
.with_target(false)
.without_time()
.with_writer(std::io::stderr);
tracing_subscriber::registry()
.with(filter_layer)
.with(format_layer)
.with(ErrorLayer::default())
.try_init()?;
Ok(())
}
@mxxntype
Copy link
Author

User-facing errors look like this:

2024-09-26T23:01:12.769132Z  INFO read_file: Attempting to read file PATH="/does/not/exist"
Error:
   0: Unable to read config
   1: No such file or directory (os error 2)

Location:
   color-eyre/examples/usage.rs:38

  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ SPANTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   0: color_eyre_usage::read_file
      at color-eyre/examples/usage.rs:32

Suggestion: try using a file that exists next time

Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.

@mxxntype
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment