Skip to content

respect color settings for log messages #11604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions crates/uv/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,24 @@ pub(crate) fn setup_logging(
.from_env()
.context("Invalid RUST_LOG directives")?;

let ansi = match color.and_colorchoice(anstream::Stderr::choice(&std::io::stderr())) {
ColorChoice::Always => true,
ColorChoice::Never => false,
ColorChoice::Auto => unreachable!("anstream can't return auto as choice"),
};
// Determine our final color settings and create an anstream wrapper based on it.
//
// The tracing `with_ansi` function on affects color tracing adds *on top of* the
// log messages. This means that if we `debug!("{}", "hello".green())`,
// (a thing we absolutely do throughout uv), then there will still be color
// in the logs, which is undesirable.
//
// So we tell tracing to print to an anstream wrapper around stderr that force-strips ansi.
// Given we do this, using `with_ansi` at all is arguably pointless, but it feels morally
// correct to still do it? I don't know what would break if we didn't... but why find out?
let (ansi, color_choice) =
match color.and_colorchoice(anstream::Stderr::choice(&std::io::stderr())) {
ColorChoice::Always => (true, anstream::ColorChoice::Always),
ColorChoice::Never => (false, anstream::ColorChoice::Never),
ColorChoice::Auto => unreachable!("anstream can't return auto as choice"),
};
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.
Expand All @@ -161,7 +174,7 @@ pub(crate) fn setup_logging(
.with(
tracing_subscriber::fmt::layer()
.event_format(format)
.with_writer(std::io::stderr)
.with_writer(writer)
.with_ansi(ansi)
.with_filter(filter),
)
Expand All @@ -175,7 +188,7 @@ pub(crate) fn setup_logging(
HierarchicalLayer::default()
.with_targets(true)
.with_timer(Uptime::default())
.with_writer(std::io::stderr)
.with_writer(writer)
.with_ansi(ansi)
.with_filter(filter),
)
Expand Down
Loading