Skip to content

Commit 17feb09

Browse files
authored
Log panics through tracing so they reach the rolling log file (#241)
Rust's default panic hook writes only to stderr, so panics never reached the rolling file appender (logs/beacon.log.*), making post-mortem debugging from log files impossible. Install a panic hook in async_main() (right after setup_tracing) that routes panics through `tracing` at ERROR level via the tracing-panic crate, while chaining the default hook to preserve stderr output. Panics now appear in both stdout and the rolling log file with message, location, and (when RUST_BACKTRACE is set) a backtrace. Closes #239
1 parent 6c100fd commit 17feb09

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

beacon-api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ uuid = { version = "1.16.0" }
3333
tracing = { workspace = true}
3434
tracing-subscriber = { workspace = true}
3535
tracing-appender = { workspace = true}
36+
tracing-panic = "0.1"
3637
tonic = "0.14.1"
3738
prost = "0.14.1"
3839

beacon-api/src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ fn main() -> anyhow::Result<()> {
3939
/// Initializes shared services and starts all configured API transports.
4040
async fn async_main() -> anyhow::Result<()> {
4141
setup_tracing();
42+
install_panic_hook();
4243

4344
tracing::info!("Beacon v{}", BEACON_VERSION);
4445
let beacon_runtime = Arc::new(beacon_core::runtime::Runtime::new().await?);
@@ -81,6 +82,16 @@ async fn serve_http(router: ::axum::Router, addr: std::net::SocketAddr) -> anyho
8182
Ok(())
8283
}
8384

85+
/// Routes panics through `tracing` (so they land in the rolling log file) while
86+
/// preserving the default hook's stderr output.
87+
fn install_panic_hook() {
88+
let default_hook = std::panic::take_hook();
89+
std::panic::set_hook(Box::new(move |info| {
90+
tracing_panic::panic_hook(info); // ERROR event -> stdout + rolling log file
91+
default_hook(info); // preserve default stderr output
92+
}));
93+
}
94+
8495
/// Configures stdout and rolling-file tracing subscribers for the API process.
8596
fn setup_tracing() {
8697
let file_appender = tracing_appender::rolling::daily("logs", "beacon.log");

0 commit comments

Comments
 (0)