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 = "*", 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 dontpanic = 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();
    dontpanic.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 = "*", features = ["tracing"] }

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

use anyhow::Result;

use tracing_subscriber::prelude::*;

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

    tracing_subscriber::registry()
        .with(
            tracing_subscriber::fmt::layer()
                .with_span_events(tracing_subscriber::fmt::format::FmtSpan::CLOSE)
                .json(),
        )
        .with(dontpanic.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§

Builder
A builder to configure dontpanic behavior.
Client
dontpanic library client.
TracingLayer
A tracing Layer implementation that records tracing events.

Enums§

Error
The Errors that may occur when interacting with this library

Functions§

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