Crate dontpanic

source ·
Expand description

Client library for Don’t Panic Server

This crate registers a panic handler and send each panic from your application to a backend server. If configured, the latest log messages before each panic are sent as well. Supported logging facilities are log and tracing. By default log::error! and tracing::error! will also send a report to the Don’t Panic Server.

Minimum usage example:

use anyhow::Result;

fn main() -> Result<()> {
    dontpanic::builder("<PROJECT_API_KEY>").build()?

    // panic!
    Option::<u32>::None.unwrap();

    Ok(())
}

§Using dontpanic with log

Tracking down the source of a panic is easier when having the logs leading up to it. Make sure to include this library with the log feature enabled:

[dependencies]
dontpanic = { version = "0.1", features = ["log"] }

Then, Client::set_logger will accept any valid logging implementation. Initialize everything as early as possible, as panics before initialization won’t be caught.

use anyhow::Result;

fn main() -> Result<()> {
    let client = dontpanic::builder("<PROJECT_API_KEY>")
        .environment("production")
        .version(env!("CARGO_PKG_VERSION"))
        .build()?

    // Important: call .build() not .init()
    let logger = env_logger::Builder::from_default_env().build();
    client.set_logger(logger)?;

    log::info!("What's happening here?");
    log::error!("Booooom");

    // panic!
    Option::<u32>::None.unwrap();

    Ok(())
}

To obtain a PROJECT_API_KEY, check out Don’t Panic Server documentation.

§Using dontpanic with tracing

To enable tracing support, include dontpanic with the tracing feature enabled:

[dependencies]
dontpanic = { version = "0.1", features = ["tracing"] }

Then client.tracing_layer() can be used in conjunction with any tracing subscriber. This example is with the fmt subscriber:

use anyhow::Result;

fn main() -> Result<()> {
    let client = dontpanic::builder("<PROJECT_API_KEY>")
        .environment("production")
        .version(env!("CARGO_PKG_VERSION"))
        .build()?

    tracing_subscriber::registry()
        .with(tracing_subscriber::fmt::layer())
        .with(client.tracing_layer())
        .init();

    tracing::info!("What's happening here?");
    tracing::error!("Booooom");
    tracing::event!(Level::ERROR, the_answer = 42);

    // panic!
    Option::<u32>::None.unwrap();

    Ok(())
}

Structs§

  • A builder to configure dontpanic behavior.
  • dontpanic library client.
  • A tracing Layer implementation that records tracing events.

Enums§

  • The Errors that may occur when interacting with this library

Functions§

  • Main entrypoint to this library. Start here and call this first. Returns a Builder.