diff --git a/komorebi/src/main.rs b/komorebi/src/main.rs index d188c205..bd2e25a9 100644 --- a/komorebi/src/main.rs +++ b/komorebi/src/main.rs @@ -82,6 +82,31 @@ fn setup() -> Result { ), )?; + // https://github.com/tokio-rs/tracing/blob/master/examples/examples/panic_hook.rs + // Set a panic hook that records the panic as a `tracing` event at the + // `ERROR` verbosity level. + // + // If we are currently in a span when the panic occurred, the logged event + // will include the current span, allowing the context in which the panic + // occurred to be recorded. + std::panic::set_hook(Box::new(|panic| { + // If the panic has a source location, record it as structured fields. + if let Some(location) = panic.location() { + // On nightly Rust, where the `PanicInfo` type also exposes a + // `message()` method returning just the message, we could record + // just the message instead of the entire `fmt::Display` + // implementation, avoiding the duplciated location + tracing::error!( + message = %panic, + panic.file = location.file(), + panic.line = location.line(), + panic.column = location.column(), + ); + } else { + tracing::error!(message = %panic); + } + })); + Ok(guard) }