From b6b7caa0238b2f8fc455a3a6769f6ca9ae65c2af Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Thu, 15 May 2025 09:57:59 +0200 Subject: [PATCH] [ty] Change layout of extra verbose output and respect `--color` for verbose output (#18089) --- Cargo.lock | 25 +-------------------- Cargo.toml | 1 - crates/ruff_db/Cargo.toml | 3 +-- crates/ruff_db/src/testing.rs | 41 +++++++---------------------------- crates/ty/Cargo.toml | 1 - crates/ty/src/lib.rs | 2 +- crates/ty/src/logging.rs | 28 ++++++++++++++++-------- 7 files changed, 30 insertions(+), 71 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a967621aae..55cd417233 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1943,15 +1943,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "nu-ansi-term" -version = "0.50.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4a28e057d01f97e61255210fcff094d74ed0466038633e95017f5beb68e4399" -dependencies = [ - "windows-sys 0.52.0", -] - [[package]] name = "num-traits" version = "0.2.19" @@ -2706,7 +2697,6 @@ dependencies = [ "thiserror 2.0.12", "tracing", "tracing-subscriber", - "tracing-tree", "web-time", "zip", ] @@ -3955,7 +3945,7 @@ checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008" dependencies = [ "chrono", "matchers", - "nu-ansi-term 0.46.0", + "nu-ansi-term", "once_cell", "regex", "sharded-slab", @@ -3966,18 +3956,6 @@ dependencies = [ "tracing-log", ] -[[package]] -name = "tracing-tree" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f459ca79f1b0d5f71c54ddfde6debfc59c8b6eeb46808ae492077f739dc7b49c" -dependencies = [ - "nu-ansi-term 0.50.1", - "tracing-core", - "tracing-log", - "tracing-subscriber", -] - [[package]] name = "tryfn" version = "0.2.3" @@ -4017,7 +3995,6 @@ dependencies = [ "tracing", "tracing-flame", "tracing-subscriber", - "tracing-tree", "ty_project", "ty_python_semantic", "ty_server", diff --git a/Cargo.toml b/Cargo.toml index d28dd23848..79b8164e39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -163,7 +163,6 @@ tracing-subscriber = { version = "0.3.18", default-features = false, features = "env-filter", "fmt", ] } -tracing-tree = { version = "0.4.0" } tryfn = { version = "0.2.1" } typed-arena = { version = "2.0.2" } unic-ucd-category = { version = "0.9" } diff --git a/crates/ruff_db/Cargo.toml b/crates/ruff_db/Cargo.toml index 3507efa545..f36a49810c 100644 --- a/crates/ruff_db/Cargo.toml +++ b/crates/ruff_db/Cargo.toml @@ -36,7 +36,6 @@ path-slash = { workspace = true } thiserror = { workspace = true } tracing = { workspace = true } tracing-subscriber = { workspace = true, optional = true } -tracing-tree = { workspace = true, optional = true } rustc-hash = { workspace = true } zip = { workspace = true } @@ -55,4 +54,4 @@ cache = ["ruff_cache"] os = ["ignore", "dep:etcetera"] serde = ["dep:serde", "camino/serde1"] # Exposes testing utilities. -testing = ["tracing-subscriber", "tracing-tree"] +testing = ["tracing-subscriber"] diff --git a/crates/ruff_db/src/testing.rs b/crates/ruff_db/src/testing.rs index 57f264fa69..5848c5d194 100644 --- a/crates/ruff_db/src/testing.rs +++ b/crates/ruff_db/src/testing.rs @@ -141,7 +141,6 @@ pub fn setup_logging_with_filter(filter: &str) -> Option { #[derive(Debug)] pub struct LoggingBuilder { filter: EnvFilter, - hierarchical: bool, } impl LoggingBuilder { @@ -154,50 +153,26 @@ impl LoggingBuilder { .parse() .expect("Hardcoded directive to be valid"), ), - hierarchical: false, } } pub fn with_filter(filter: &str) -> Option { let filter = EnvFilter::builder().parse(filter).ok()?; - Some(Self { - filter, - hierarchical: false, - }) - } - - pub fn with_hierarchical(mut self, hierarchical: bool) -> Self { - self.hierarchical = hierarchical; - self + Some(Self { filter }) } pub fn build(self) -> LoggingGuard { let registry = tracing_subscriber::registry().with(self.filter); - let guard = if self.hierarchical { - let subscriber = registry.with( - tracing_tree::HierarchicalLayer::default() - .with_indent_lines(true) - .with_indent_amount(2) - .with_bracketed_fields(true) - .with_thread_ids(true) - .with_targets(true) - .with_writer(std::io::stderr) - .with_timer(tracing_tree::time::Uptime::default()), - ); + let subscriber = registry.with( + tracing_subscriber::fmt::layer() + .compact() + .with_writer(std::io::stderr) + .with_timer(tracing_subscriber::fmt::time()), + ); - tracing::subscriber::set_default(subscriber) - } else { - let subscriber = registry.with( - tracing_subscriber::fmt::layer() - .compact() - .with_writer(std::io::stderr) - .with_timer(tracing_subscriber::fmt::time()), - ); - - tracing::subscriber::set_default(subscriber) - }; + let guard = tracing::subscriber::set_default(subscriber); LoggingGuard { _guard: guard } } diff --git a/crates/ty/Cargo.toml b/crates/ty/Cargo.toml index 1a1222121d..f7fcae1212 100644 --- a/crates/ty/Cargo.toml +++ b/crates/ty/Cargo.toml @@ -35,7 +35,6 @@ salsa = { workspace = true } tracing = { workspace = true, features = ["release_max_level_debug"] } tracing-subscriber = { workspace = true, features = ["env-filter", "fmt"] } tracing-flame = { workspace = true } -tracing-tree = { workspace = true } wild = { workspace = true } [dev-dependencies] diff --git a/crates/ty/src/lib.rs b/crates/ty/src/lib.rs index a0b2e1037e..f51030c987 100644 --- a/crates/ty/src/lib.rs +++ b/crates/ty/src/lib.rs @@ -60,7 +60,7 @@ fn run_check(args: CheckCommand) -> anyhow::Result { let verbosity = args.verbosity.level(); countme::enable(verbosity.is_trace()); - let _guard = setup_tracing(verbosity)?; + let _guard = setup_tracing(verbosity, args.color.unwrap_or_default())?; tracing::warn!( "ty is pre-release software and not ready for production use. \ diff --git a/crates/ty/src/logging.rs b/crates/ty/src/logging.rs index 5119af652b..a90dfd85e7 100644 --- a/crates/ty/src/logging.rs +++ b/crates/ty/src/logging.rs @@ -1,10 +1,11 @@ //! Sets up logging for ty +use crate::args::TerminalColor; use anyhow::Context; use colored::Colorize; use std::fmt; use std::fs::File; -use std::io::BufWriter; +use std::io::{BufWriter, IsTerminal}; use tracing::{Event, Subscriber}; use tracing_subscriber::filter::LevelFilter; use tracing_subscriber::fmt::format::Writer; @@ -76,7 +77,10 @@ impl VerbosityLevel { } } -pub(crate) fn setup_tracing(level: VerbosityLevel) -> anyhow::Result { +pub(crate) fn setup_tracing( + level: VerbosityLevel, + color: TerminalColor, +) -> anyhow::Result { use tracing_subscriber::prelude::*; // The `TY_LOG` environment variable overrides the default log level. @@ -115,16 +119,21 @@ pub(crate) fn setup_tracing(level: VerbosityLevel) -> anyhow::Result { + colored::control::SHOULD_COLORIZE.should_colorize() && std::io::stderr().is_terminal() + } + TerminalColor::Always => true, + TerminalColor::Never => false, + }; + if level.is_trace() { let subscriber = registry.with( - tracing_tree::HierarchicalLayer::default() - .with_indent_lines(true) - .with_indent_amount(2) - .with_bracketed_fields(true) + tracing_subscriber::fmt::layer() + .event_format(tracing_subscriber::fmt::format().pretty()) .with_thread_ids(true) - .with_targets(true) - .with_writer(std::io::stderr) - .with_timer(tracing_tree::time::Uptime::default()), + .with_ansi(ansi) + .with_writer(std::io::stderr), ); subscriber.init(); @@ -136,6 +145,7 @@ pub(crate) fn setup_tracing(level: VerbosityLevel) -> anyhow::Result