[ty] Change layout of extra verbose output and respect --color for verbose output (#18089)

This commit is contained in:
Micha Reiser 2025-05-15 09:57:59 +02:00 committed by GitHub
parent 46be305ad2
commit b6b7caa023
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 30 additions and 71 deletions

25
Cargo.lock generated
View file

@ -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",

View file

@ -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" }

View file

@ -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"]

View file

@ -141,7 +141,6 @@ pub fn setup_logging_with_filter(filter: &str) -> Option<LoggingGuard> {
#[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<Self> {
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 }
}

View file

@ -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]

View file

@ -60,7 +60,7 @@ fn run_check(args: CheckCommand) -> anyhow::Result<ExitStatus> {
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. \

View file

@ -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<TracingGuard> {
pub(crate) fn setup_tracing(
level: VerbosityLevel,
color: TerminalColor,
) -> anyhow::Result<TracingGuard> {
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<TracingGuar
.with(filter)
.with(profiling_layer);
let ansi = match color {
TerminalColor::Auto => {
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<TracingGuar
display_timestamp: level.is_extra_verbose(),
show_spans: false,
})
.with_ansi(ansi)
.with_writer(std::io::stderr),
);