#logging-tracing #error-logging #error-handling #telemetry

tracing_log_error

A set of helpers to capture rich error context in tracing logs

5 releases

0.1.4 Jan 21, 2025
0.1.3 Jan 21, 2025
0.1.2 Jan 21, 2025
0.1.1 Jan 20, 2025
0.1.0 Jan 20, 2025

#811 in Debugging

Download history 565/week @ 2025-10-01 177/week @ 2025-10-08 191/week @ 2025-10-15 173/week @ 2025-10-22 170/week @ 2025-10-29 285/week @ 2025-11-05 243/week @ 2025-11-12 140/week @ 2025-11-19 123/week @ 2025-11-26 142/week @ 2025-12-03 164/week @ 2025-12-10 381/week @ 2025-12-17 194/week @ 2025-12-24 134/week @ 2025-12-31 152/week @ 2026-01-07 289/week @ 2026-01-14

798 downloads per month
Used in 13 crates (6 directly)

MIT/Apache

16KB
165 lines

tracing_log_error

A utility crate to capture an error, and all its key error properties, in a tracing event.

use tracing_log_error::log_error;

let e = std::io::Error::new(std::io::ErrorKind::Other, "My error");
log_error!(e, "The connection was dropped");

The log_error! invocation captures:

  • The Display representation of the error, in the error.message field.
  • The Debug representation of the error, in the error.details field.
  • The chain of error sources, in the error.source_chain field.

Using raw tracing, the equivalent would be:

use tracing::{event, Level};
use tracing_log_error::fields;

let e = std::io::Error::new(std::io::ErrorKind::Other, "My error");
event!(
    Level::ERROR,
    error.message = fields::error_message(&e),
    error.details = fields::error_details(&e),
    error.source_chain = fields::error_source_chain(&e),
    "The connection was dropped"
);

Installation

To use log_error!, add both tracing and tracing_log_error to your Cargo.toml:

[dependencies]
tracing = "0.1"
tracing-log-error = "0.1"

Some errors don't implement the Error trait

Some common error reporting types, like anyhow::Error or eyre::Report or Box<dyn std::error::Error>, don't implement the Error trait. If you try to use log_error! with them directly, you'll get a compiler error.

Good news: you can still use log_error! with them! They dereference to a type that implements the Error trait, so you can use * to dereference them when passing them to log_error!:

use tracing_log_error::log_error;
use anyhow::anyhow;

let e = anyhow!("Hey");
// Notice the `*` 👇
log_error!(*e, "An error occurred");

Advanced usage

Check out log_error!'s documentation for more examples and details. You can customize the log level, add custom fields, and more.

Dependencies

~5–7MB
~49K SLoC