Skip to content

Commit f394f72

Browse files
authored
respect color settings for log messages (#11604)
fixes #11560
1 parent 0d29382 commit f394f72

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

crates/uv/src/logging.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,24 @@ pub(crate) fn setup_logging(
142142
.from_env()
143143
.context("Invalid RUST_LOG directives")?;
144144

145-
let ansi = match color.and_colorchoice(anstream::Stderr::choice(&std::io::stderr())) {
146-
ColorChoice::Always => true,
147-
ColorChoice::Never => false,
148-
ColorChoice::Auto => unreachable!("anstream can't return auto as choice"),
149-
};
145+
// Determine our final color settings and create an anstream wrapper based on it.
146+
//
147+
// The tracing `with_ansi` function on affects color tracing adds *on top of* the
148+
// log messages. This means that if we `debug!("{}", "hello".green())`,
149+
// (a thing we absolutely do throughout uv), then there will still be color
150+
// in the logs, which is undesirable.
151+
//
152+
// So we tell tracing to print to an anstream wrapper around stderr that force-strips ansi.
153+
// Given we do this, using `with_ansi` at all is arguably pointless, but it feels morally
154+
// correct to still do it? I don't know what would break if we didn't... but why find out?
155+
let (ansi, color_choice) =
156+
match color.and_colorchoice(anstream::Stderr::choice(&std::io::stderr())) {
157+
ColorChoice::Always => (true, anstream::ColorChoice::Always),
158+
ColorChoice::Never => (false, anstream::ColorChoice::Never),
159+
ColorChoice::Auto => unreachable!("anstream can't return auto as choice"),
160+
};
161+
let writer = std::sync::Mutex::new(anstream::AutoStream::new(std::io::stderr(), color_choice));
162+
150163
match level {
151164
Level::Default | Level::Verbose => {
152165
// Regardless of the tracing level, show messages without any adornment.
@@ -161,7 +174,7 @@ pub(crate) fn setup_logging(
161174
.with(
162175
tracing_subscriber::fmt::layer()
163176
.event_format(format)
164-
.with_writer(std::io::stderr)
177+
.with_writer(writer)
165178
.with_ansi(ansi)
166179
.with_filter(filter),
167180
)
@@ -175,7 +188,7 @@ pub(crate) fn setup_logging(
175188
HierarchicalLayer::default()
176189
.with_targets(true)
177190
.with_timer(Uptime::default())
178-
.with_writer(std::io::stderr)
191+
.with_writer(writer)
179192
.with_ansi(ansi)
180193
.with_filter(filter),
181194
)

0 commit comments

Comments
 (0)