From 36d4403bd5a45407b68d74dca9e005afe46c4346 Mon Sep 17 00:00:00 2001 From: Tanmay Maheshwari Date: Wed, 31 Dec 2025 12:57:43 +0530 Subject: [PATCH 1/3] feat(cli): show source file locations only in debug mode - parse RUST_LOG env var and normalize to lowercase - detect debug mode by checking for any substring matching `=debug` - construct EnvFilter from RUST_LOG with fallback to "info" if unset - configure tracing_subscriber::fmt::Layer with conditional toggles: * with_file(true/false) * with_line_number(true/false) * with_target(true/false) - registry initialized with fmt_layer to apply formatting globally Implements enhancement requested in #403 --- crates/cli/src/main.rs | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 202d5d5f..9c56929d 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -25,7 +25,6 @@ use error::CliError; use std::{str::FromStr, sync::LazyLock}; use tokio::sync::OnceCell; use tracing::{debug, info, warn}; -use tracing_subscriber::EnvFilter; use util::{data_dir, db_file, init_reports_dir}; static DB: LazyLock = std::sync::LazyLock::new(|| { @@ -209,27 +208,21 @@ fn init_db(command: &ContenderSubcommand) -> Result<(), CliError> { } fn init_tracing() { - let filter = EnvFilter::try_from_default_env().ok(); // fallback if RUST_LOG is unset - #[cfg(feature = "async-tracing")] - { - use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, Layer}; - let tokio_layer = console_subscriber::ConsoleLayer::builder() - .with_default_env() - .spawn(); - let fmt_layer = fmt::layer() - .with_ansi(true) - .with_target(true) - .with_line_number(true) - .with_filter(filter); - - tracing_subscriber::Registry::default() - .with(fmt_layer) - .with(tokio_layer) - .init(); - } + use tracing_subscriber::{ + fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer, + }; - #[cfg(not(feature = "async-tracing"))] - { - contender_core::util::init_core_tracing(filter); - } + let rust_log = std::env::var("RUST_LOG").unwrap_or_default().to_lowercase(); + let debug_mode = rust_log.contains("=debug"); + + let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")); + + let fmt_layer = fmt::layer() + .with_ansi(true) + .with_target(debug_mode) + .with_line_number(debug_mode) + .with_file(debug_mode) + .with_filter(filter); + + tracing_subscriber::registry().with(fmt_layer).init(); } From 8280d66ed17b1bd9eb83524a78ed889147b93cb4 Mon Sep 17 00:00:00 2001 From: Tanmay Maheshwari Date: Wed, 31 Dec 2025 13:26:45 +0530 Subject: [PATCH 2/3] test(cli): add scoped env var tests for debug mode detection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add temp-env as a dev-dependency - add #[cfg(test)] module in crates/cli/src/main.rs - use temp_env::with_var to set RUST_LOG temporarily - verify debug mode detection logic: * RUST_LOG="contender=debug" → assert contains("=debug") * RUST_LOG="contender=info" → assert !contains("=debug") - ensures environment isolation between tests without global state leakage Adds test case for the enhancement implemented in #403 --- Cargo.lock | 10 ++++++++++ crates/cli/Cargo.toml | 1 + crates/cli/src/main.rs | 21 +++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 6a69af32..0b266043 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2164,6 +2164,7 @@ dependencies = [ "serde", "serde_json", "strum", + "temp-env", "tempfile", "thiserror 2.0.17", "tokio", @@ -9918,6 +9919,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" +[[package]] +name = "temp-env" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96374855068f47402c3121c6eed88d29cb1de8f3ab27090e273e420bdabcf050" +dependencies = [ + "parking_lot", +] + [[package]] name = "tempfile" version = "3.23.0" diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index c828ae64..79a659da 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -51,6 +51,7 @@ uuid = { workspace = true } [dev-dependencies] tempfile = "3.15.0" +temp-env = "0.3" [features] default = [] diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 9c56929d..92ad1e55 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -226,3 +226,24 @@ fn init_tracing() { tracing_subscriber::registry().with(fmt_layer).init(); } + +#[cfg(test)] +mod tests { + use temp_env::with_var; + + #[test] + fn debug_mode_detected_from_rust_log() { + with_var("RUST_LOG", Some("contender=debug"), || { + let rust_log = std::env::var("RUST_LOG").unwrap_or_default().to_lowercase(); + assert!(rust_log.contains("=debug")); + }); + } + + #[test] + fn info_mode_does_not_trigger_debug() { + with_var("RUST_LOG", Some("contender=info"), || { + let rust_log = std::env::var("RUST_LOG").unwrap_or_default().to_lowercase(); + assert!(!rust_log.contains("=debug")); + }); + } +} From 51092d08b10d2eeb9cc6d9c6aa1d1354f9b6089c Mon Sep 17 00:00:00 2001 From: Tanmay Maheshwari Date: Wed, 31 Dec 2025 14:08:51 +0530 Subject: [PATCH 3/3] docs(changelog): update root and cli changelogs for debug mode logging - add entry under Unreleased in crates/cli/CHANGELOG.md - add corresponding summary under Unreleased in root CHANGELOG.md Adds changelog entries for enhancement implemented in #403 --- CHANGELOG.md | 9 ++++++++- crates/cli/CHANGELOG.md | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c0aa209..e165287e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 > Note: this file did not exist until after `v0.5.6`. +## Unreleased + +### CLI +- Logging: show source file locations only when running in debug mode (`RUST_LOG=*=debug`) ([#403](https://github.com/flashbots/contender/issues/403)) +- Tests: add scoped environment variable tests for debug mode detection using `temp-env` + + --- ## [0.6.0](https://github.com/flashbots/contender/releases/tag/v0.6.0) - 2025-11-25 @@ -22,4 +29,4 @@ Internal changes: - revamp error handling ([#378](https://github.com/flashbots/contender/pull/378)) - DB schema bumped to `user_version = 6` to record campaign/stage metadata in runs. - - If you see a DB version mismatch, export/reset your DB: `contender db export` (optional backup) then `contender db reset` (or `drop`) to recreate with the new schema. \ No newline at end of file + - If you see a DB version mismatch, export/reset your DB: `contender db export` (optional backup) then `contender db reset` (or `drop`) to recreate with the new schema. diff --git a/crates/cli/CHANGELOG.md b/crates/cli/CHANGELOG.md index bf593304..ac1299cc 100644 --- a/crates/cli/CHANGELOG.md +++ b/crates/cli/CHANGELOG.md @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - we no longer require two-phase cancellation (CTRL-C once to stop spamming, CTRL-C again to stop result collection) - result collection happens async, so when the user cancels, most results will have already been collected - stopping quickly is a better UX than two-phase +- logging: show source file locations only when running in debug mode (`RUST_LOG=*=debug`) ([#403](https://github.com/flashbots/contender/issues/403)) +- tests: add scoped environment variable tests for debug mode detection using `temp-env` ### Breaking changes