color-eyre
color-eyre copied to clipboard
usage.rs minimal example does not print info event, no span trace either
When I copy paste the source code of usage.rs
into a new minimal rust project, it does not show the INFO
trace event and the error does not have the SPANTRACE
section.
The Cargo.toml of that minimal project looks like this:
[package]
name = "minimal"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
color-eyre = {version = "0.6.2", features = ["capture-spantrace"]}
tracing = "0.1.36"
tracing-subscriber = "0.3.15"
tracing-error = "0.2.0"
But when I run cargo run --example usage
in the local clone of the color-eyre
repository, I see the Info Event being print out just just fine:
2022-08-16T06:47:07.017504Z INFO read_config:read_file{path="fake_file"}: Reading file
And the Error is shown with its span trace section:
...
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ SPANTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
0: usage::read_file with path="fake_file"
at examples\usage.rs:32
1: usage::read_config
at examples\usage.rs:38
...
What am I missing in the minimal example?
If you copy the source text directly, then you'll include the #[cfg(feature = "capture-spantrace")]
, which won't work unless you've defined your own feature named the same. Try removing the directives, i.e. write this:
use color_eyre::{eyre::Report, eyre::WrapErr, Section};
use tracing::{info, instrument};
#[instrument]
fn main() -> Result<(), Report> {
install_tracing();
color_eyre::install()?;
read_config()
}
fn install_tracing() {
use tracing_error::ErrorLayer;
use tracing_subscriber::prelude::*;
use tracing_subscriber::{fmt, EnvFilter};
let fmt_layer = fmt::layer().with_target(false);
let filter_layer = EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new("info"))
.unwrap();
tracing_subscriber::registry()
.with(filter_layer)
.with(fmt_layer)
.with(ErrorLayer::default())
.init();
}
#[instrument]
fn read_file(path: &str) -> Result<(), Report> {
info!("Reading file");
Ok(std::fs::read_to_string(path).map(drop)?)
}
#[instrument]
fn read_config() -> Result<(), Report> {
read_file("fake_file")
.wrap_err("Unable to read config")
.suggestion("try using a file that exists next time")
}
And then also enable the env-filter
feature of tracing-subscriber
so that the code builds:
[package]
name = "minimal"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
color-eyre = {version = "0.6.2", features = ["capture-spantrace"]}
tracing = "0.1.36"
tracing-subscriber = { version = "0.3.15", features = ["env-filter"] }
tracing-error = "0.2.0"
Then it works for me.
Thanks, I'll check it out in the course of the next week.