From bcb0bf6de7a7c4ed93171fdbb2f2b5a323e46bdf Mon Sep 17 00:00:00 2001 From: Aria Desires Date: Mon, 24 Feb 2025 16:32:44 -0500 Subject: [PATCH 1/3] rework log verbosity () --- crates/uv-static/src/env_vars.rs | 3 + crates/uv/src/lib.rs | 7 ++- crates/uv/src/logging.rs | 94 +++++++++++++++++--------------- 3 files changed, 57 insertions(+), 47 deletions(-) diff --git a/crates/uv-static/src/env_vars.rs b/crates/uv-static/src/env_vars.rs index 7d54ba2660e6..4aec3ab678e7 100644 --- a/crates/uv-static/src/env_vars.rs +++ b/crates/uv-static/src/env_vars.rs @@ -548,6 +548,9 @@ impl EnvVars { /// for more. pub const RUST_LOG: &'static str = "RUST_LOG"; + /// Give log messages more detailed context + pub const UV_LOG_CONTEXT: &'static str = "UV_LOG_CONTEXT"; + /// Use to set the stack size used by uv. /// /// The value is in bytes, and the default is typically 2MB (2097152). diff --git a/crates/uv/src/lib.rs b/crates/uv/src/lib.rs index 30966ee1d75a..39557f7e3818 100644 --- a/crates/uv/src/lib.rs +++ b/crates/uv/src/lib.rs @@ -279,9 +279,10 @@ async fn run(mut cli: Cli) -> Result { let duration_layer = None::; logging::setup_logging( match globals.verbose { - 0 => logging::Level::Default, - 1 => logging::Level::Verbose, - 2.. => logging::Level::ExtraVerbose, + 0 => logging::Level::Off, + 1 => logging::Level::DebugUv, + 2 => logging::Level::TraceUv, + 3.. => logging::Level::TraceAll, }, duration_layer, globals.color, diff --git a/crates/uv/src/logging.rs b/crates/uv/src/logging.rs index a8e24a05ca5e..8ba57aa8ae70 100644 --- a/crates/uv/src/logging.rs +++ b/crates/uv/src/logging.rs @@ -20,18 +20,17 @@ use tracing_tree::time::Uptime; use tracing_tree::HierarchicalLayer; use uv_cli::ColorChoice; +use uv_static::EnvVars; #[cfg(feature = "tracing-durations-export")] use uv_static::EnvVars; #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] pub(crate) enum Level { - /// Suppress all tracing output by default (overridable by `RUST_LOG`). #[default] - Default, - /// Show debug messages by default (overridable by `RUST_LOG`). - Verbose, - /// Show messages in a hierarchical span tree. By default, debug messages are shown (overridable by `RUST_LOG`). - ExtraVerbose, + Off, + DebugUv, + TraceUv, + TraceAll, } struct UvFormat { @@ -120,15 +119,24 @@ pub(crate) fn setup_logging( durations: impl Layer + Send + Sync, color: ColorChoice, ) -> anyhow::Result<()> { + // We use directives here to ensure `RUST_LOG` can override them let default_directive = match level { - Level::Default => { - // Show nothing, but allow `RUST_LOG` to override. + Level::Off => { + // Show nothing tracing::level_filters::LevelFilter::OFF.into() } - Level::Verbose | Level::ExtraVerbose => { - // Show `DEBUG` messages from the CLI crate, but allow `RUST_LOG` to override. + Level::DebugUv => { + // Show `DEBUG` messages from the CLI crate Directive::from_str("uv=debug").unwrap() } + Level::TraceUv => { + // Show `TRACE` messages from the CLI crate, but allow `RUST_LOG` to override. + Directive::from_str("uv=trace").unwrap() + } + Level::TraceAll => { + // Show `TRACE` messages from the CLI crate, but allow `RUST_LOG` to override. + Directive::from_str("trace").unwrap() + } }; // Only record our own spans. @@ -160,40 +168,38 @@ pub(crate) fn setup_logging( }; let writer = std::sync::Mutex::new(anstream::AutoStream::new(std::io::stderr(), color_choice)); - match level { - Level::Default | Level::Verbose => { - // Regardless of the tracing level, show messages without any adornment. - let format = UvFormat { - display_timestamp: false, - display_level: true, - show_spans: false, - }; - - tracing_subscriber::registry() - .with(durations_layer) - .with( - tracing_subscriber::fmt::layer() - .event_format(format) - .with_writer(writer) - .with_ansi(ansi) - .with_filter(filter), - ) - .init(); - } - Level::ExtraVerbose => { - // Regardless of the tracing level, include the uptime and target for each message. - tracing_subscriber::registry() - .with(durations_layer) - .with( - HierarchicalLayer::default() - .with_targets(true) - .with_timer(Uptime::default()) - .with_writer(writer) - .with_ansi(ansi) - .with_filter(filter), - ) - .init(); - } + let detailed_logging = std::env::var(EnvVars::UV_LOG_CONTEXT).is_ok(); + if detailed_logging { + // Regardless of the tracing level, include the uptime and target for each message. + tracing_subscriber::registry() + .with(durations_layer) + .with( + HierarchicalLayer::default() + .with_targets(true) + .with_timer(Uptime::default()) + .with_writer(writer) + .with_ansi(ansi) + .with_filter(filter), + ) + .init(); + } else { + // Regardless of the tracing level, show messages without any adornment. + let format = UvFormat { + display_timestamp: false, + display_level: true, + show_spans: false, + }; + + tracing_subscriber::registry() + .with(durations_layer) + .with( + tracing_subscriber::fmt::layer() + .event_format(format) + .with_writer(writer) + .with_ansi(ansi) + .with_filter(filter), + ) + .init(); } Ok(()) From e162ba6eda498b344b71cf2423f5640365ae6f83 Mon Sep 17 00:00:00 2001 From: Aria Desires Date: Mon, 24 Feb 2025 16:42:57 -0500 Subject: [PATCH 2/3] fix and regen --- crates/uv/src/logging.rs | 6 +++--- docs/configuration/environment.md | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/uv/src/logging.rs b/crates/uv/src/logging.rs index 8ba57aa8ae70..327dbaf6f634 100644 --- a/crates/uv/src/logging.rs +++ b/crates/uv/src/logging.rs @@ -126,15 +126,15 @@ pub(crate) fn setup_logging( tracing::level_filters::LevelFilter::OFF.into() } Level::DebugUv => { - // Show `DEBUG` messages from the CLI crate + // Show `DEBUG` messages from the CLI crate (and ERROR/WARN/INFO) Directive::from_str("uv=debug").unwrap() } Level::TraceUv => { - // Show `TRACE` messages from the CLI crate, but allow `RUST_LOG` to override. + // Show `TRACE` messages from the CLI crate (and ERROR/WARN/INFO/DEBUG) Directive::from_str("uv=trace").unwrap() } Level::TraceAll => { - // Show `TRACE` messages from the CLI crate, but allow `RUST_LOG` to override. + // Show all `TRACE` messages (and ERROR/WARN/INFO/DEBUG) Directive::from_str("trace").unwrap() } }; diff --git a/docs/configuration/environment.md b/docs/configuration/environment.md index 99a5a375a95a..5b8bb70a8784 100644 --- a/docs/configuration/environment.md +++ b/docs/configuration/environment.md @@ -173,6 +173,10 @@ a link mode. Equivalent to the `--locked` command-line argument. If set, uv will assert that the `uv.lock` remains unchanged. +### `UV_LOG_CONTEXT` + +Give log messages more detailed context + ### `UV_NATIVE_TLS` Equivalent to the `--native-tls` command-line argument. If set to `true`, uv will From 61eea8a6716ddfe0299e6530f05525bd13790105 Mon Sep 17 00:00:00 2001 From: Aria Desires Date: Mon, 24 Feb 2025 16:52:54 -0500 Subject: [PATCH 3/3] remove redundant import --- crates/uv/src/logging.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/uv/src/logging.rs b/crates/uv/src/logging.rs index 327dbaf6f634..49abd88645a4 100644 --- a/crates/uv/src/logging.rs +++ b/crates/uv/src/logging.rs @@ -21,8 +21,6 @@ use tracing_tree::HierarchicalLayer; use uv_cli::ColorChoice; use uv_static::EnvVars; -#[cfg(feature = "tracing-durations-export")] -use uv_static::EnvVars; #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] pub(crate) enum Level {