tracing-test icon indicating copy to clipboard operation
tracing-test copied to clipboard

opt-in triggering panic on `Level::ERROR` traces

Open urben1680 opened this issue 11 months ago • 0 comments

When working with complicated tests where deep in the code error logs are written, it would be useful if this crate offered a function that sets a tracing subscriber that panics on error events.

I wrote a naive one myself for tests of a bevy-related crate that looks like this:

fn panic_on_error_events() {
    struct PanicOnError;
    impl<S: Subscriber> Layer<S> for PanicOnError {
        fn on_event(&self, event: &Event, _ctx: Context<S>) {
            if *event.metadata().level() == Level::ERROR {
                panic!("{event:#?}")
            }
        }
    }
    if registry().with(PanicOnError).try_init().is_err() {
        get_default(|subscriber| {
            assert!(subscriber.downcast_ref::<PanicOnError>().is_some());
        })
    }
}

urben1680 avatar Jan 29 '25 14:01 urben1680