From 9b77a8873e028612bc2dbeff128178c9f3fdc235 Mon Sep 17 00:00:00 2001 From: konsti Date: Thu, 4 Jan 2024 16:43:44 +0100 Subject: [PATCH] Disable color output when redirecting stderr (#742) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I'm still confused about it, but this seems to do the right thing? `HierarchicalLayer` internally has [`let ansi = io::stderr().is_terminal();`](https://github.com/davidbarsky/tracing-tree/blob/fcd9eed2528e41cd776b19b9ba45d338ae62a5fc/src/lib.rs#L74), so the logging itself is already correctly uncolored, but errors in the log weren't. Test command, ran with network deactivated: ```shell RUST_LOG=debug cargo run --bin puffin -- pip-compile -v ./scripts/popular_packages/pypi_8k_downloads.txt 2> log.txt ``` **Before** ``` error: Request error: error sending request for url (https://pypi.org/simple/apache-airflow-providers-dbt-cloud/): error trying to connect: dns error: failed to lookup address information: Temporary failure in name resolution Caused by: error sending request for url (https://pypi.org/simple/apache-airflow-providers-dbt-cloud/): error trying to connect: dns error: failed to lookup address information: Temporary failure in name resolution Caused by: error trying to connect: dns error: failed to lookup address information: Temporary failure in name resolution Caused by: dns error: failed to lookup address information: Temporary failure in name resolution Caused by: failed to lookup address information: Temporary failure in name resolution ``` **After** ``` error: Request error: error sending request for url (https://pypi.org/simple/fissix/): error trying to connect: dns error: failed to lookup address information: Temporary failure in name resolution Caused by: error sending request for url (https://pypi.org/simple/fissix/): error trying to connect: dns error: failed to lookup address information: Temporary failure in name resolution Caused by: error trying to connect: dns error: failed to lookup address information: Temporary failure in name resolution Caused by: dns error: failed to lookup address information: Temporary failure in name resolution Caused by: failed to lookup address information: Temporary failure in name resolution ``` --- crates/puffin-cli/src/logging.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/puffin-cli/src/logging.rs b/crates/puffin-cli/src/logging.rs index 5a69e4a83..478e01c0b 100644 --- a/crates/puffin-cli/src/logging.rs +++ b/crates/puffin-cli/src/logging.rs @@ -1,3 +1,4 @@ +use std::io::IsTerminal; use tracing::level_filters::LevelFilter; use tracing_subscriber::layer::SubscriberExt; use tracing_subscriber::util::SubscriberInitExt; @@ -21,6 +22,10 @@ pub(crate) enum Level { /// environment variable) along with the formatting of the output. For example, [`Level::Verbose`] /// includes targets and timestamps, along with all `puffin=debug` messages by default. pub(crate) fn setup_logging(level: Level) { + if !std::io::stderr().is_terminal() { + colored::control::set_override(false); + } + match level { Level::Default => { // Show nothing, but allow `RUST_LOG` to override.