Skip to content

Commit

Permalink
Enforce single tracing subscriber even during tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan committed Jan 8, 2025
1 parent 6217f2a commit 07398e0
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions crates/lsp/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,26 +204,18 @@ pub(crate) fn init_logging(

let subscriber = tracing_subscriber::Registry::default().with(layer);

if is_test_client(client_info) {
// During parallel testing, `set_global_default()` gets called multiple times
// per process. That causes it to error, but we ignore this.
tracing::subscriber::set_global_default(subscriber).ok();
} else {
tracing::subscriber::set_global_default(subscriber)
.expect("Should be able to set the global subscriber exactly once.");
}
tracing::subscriber::set_global_default(subscriber)
.expect("Should be able to set the global subscriber exactly once.");

tracing::info!("Logging initialized with level: {log_level}");
}

/// We use a special `TestWriter` during tests to be compatible with `cargo test`'s
/// typical output capturing behavior.
///
/// Important notes:
/// - `cargo test` swallows all logs unless you use `-- --nocapture`.
/// - Tests run in parallel, so logs can be interleaved unless you run `--test-threads 1`.
/// typical output capturing behavior (even during integration tests!).
///
/// We use `cargo test -- --nocapture --test-threads 1` on CI because of all of this.
/// Importantly, note that `cargo test` swallows all logs unless you use `-- --nocapture`,
/// which is the correct expected behavior. We use `cargo test -- --nocapture` on CI
/// because of this.
fn is_test_client(client_info: Option<&ClientInfo>) -> bool {
client_info.map_or(false, |client_info| client_info.name == "AirTestClient")
}
Expand Down

0 comments on commit 07398e0

Please sign in to comment.