[GH-ISSUE #2445] Replace commented-out env_logger lines with tracing_subscriber registration #994

Closed
opened 2026-03-16 01:12:23 +03:00 by kerem · 1 comment
Owner

Originally created by @divergentdave on GitHub (Sep 9, 2024).
Original GitHub issue: https://github.com/hickory-dns/hickory-dns/issues/2445

There are a number of tests throughout the codebase that have commented-out env_logger initializations. However, neither env_logger nor log are in use anymore. These could be replaced with calls to a function like that below, in order to make debugging test failures simpler to get started with.

/// Registers a global default tracing subscriber when called for the first time. This is intended
/// for use in tests.
fn subscribe() {
    static INSTALL_TRACING_SUBSCRIBER: Once = Once::new();
    INSTALL_TRACING_SUBSCRIBER.call_once(|| {
        let subscriber = tracing_subscriber::FmtSubscriber::builder()
            .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
            .with_test_writer()
            .finish();
        tracing::subscriber::set_global_default(subscriber).unwrap();
    });
}

The std::sync::Once makes this safe to use from multiple integration tests/unit tests at once, and .with_test_writer() redirects output through the standard library's macros, instead of bare file descriptors, so that the test harness can still capture log output.

With the current crate structure, this would have to be copied and pasted in a large number of places. #2444 would help cut that number down to one or two copies per crate. (one in src/lib.rs, and one in the sole integration test target's root module) Alternately, this function could be placed in a new crate that is added to the dev-dependencies of every crate that needs it.

Originally created by @divergentdave on GitHub (Sep 9, 2024). Original GitHub issue: https://github.com/hickory-dns/hickory-dns/issues/2445 There are a number of tests throughout the codebase that have commented-out `env_logger` initializations. However, neither `env_logger` nor `log` are in use anymore. These could be replaced with calls to a function like that below, in order to make debugging test failures simpler to get started with. ```rust /// Registers a global default tracing subscriber when called for the first time. This is intended /// for use in tests. fn subscribe() { static INSTALL_TRACING_SUBSCRIBER: Once = Once::new(); INSTALL_TRACING_SUBSCRIBER.call_once(|| { let subscriber = tracing_subscriber::FmtSubscriber::builder() .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) .with_test_writer() .finish(); tracing::subscriber::set_global_default(subscriber).unwrap(); }); } ``` The `std::sync::Once` makes this safe to use from multiple integration tests/unit tests at once, and `.with_test_writer()` redirects output through the standard library's macros, instead of bare file descriptors, so that the test harness can still capture log output. With the current crate structure, this would have to be copied and pasted in a large number of places. #2444 would help cut that number down to one or two copies per crate. (one in src/lib.rs, and one in the sole integration test target's root module) Alternately, this function could be placed in a new crate that is added to the dev-dependencies of every crate that needs it.
kerem closed this issue 2026-03-16 01:12:28 +03:00
Author
Owner

@djc commented on GitHub (Sep 10, 2024):

Sounds good -- agreed that it would be better to merge more of the integration test stack.

<!-- gh-comment-id:2340222298 --> @djc commented on GitHub (Sep 10, 2024): Sounds good -- agreed that it would be better to merge more of the integration test stack.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hickory-dns#994
No description provided.