chore: Do not use ANSI colorization if not connected to a tty (#5674)

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
pull/24376/head
Andrew Lamb 2022-09-20 07:01:58 -04:00 committed by GitHub
parent 786711f244
commit 832f305b9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 3 deletions

1
Cargo.lock generated
View File

@ -5484,6 +5484,7 @@ dependencies = [
name = "trogging"
version = "0.1.0"
dependencies = [
"atty",
"clap",
"logfmt",
"observability_deps",

View File

@ -8,6 +8,7 @@ description = "IOx logging pipeline built upon tokio-tracing"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
atty = "0.2.14"
clap = { version = "3", features = ["derive", "env"], optional = true }
logfmt = { path = "../logfmt" }
observability_deps = { path = "../observability_deps" }

View File

@ -52,7 +52,7 @@ pub enum Error {
pub type Result<T, E = Error> = std::result::Result<T, E>;
/// Builder for tracing and logging.
/// Builder to configure tracing and logging.
#[derive(Debug)]
pub struct Builder<W = fn() -> io::Stdout> {
log_format: LogFormat,
@ -72,7 +72,8 @@ impl Default for Builder {
default_log_filter: EnvFilter::try_new(Self::DEFAULT_LOG_FILTER).unwrap(),
make_writer: io::stdout,
with_target: true,
with_ansi: true,
// use ansi control codes for color if connected to a TTY
with_ansi: atty::is(atty::Stream::Stdout),
}
}
}
@ -185,7 +186,8 @@ where
/// Enable/disable ANSI encoding for formatted events (i.e. colors).
///
/// Defaults to true. See [tracing_subscriber::fmt::Layer::with_ansi]
/// Defaults to true if connected to a TTY, false otherwise. See
/// [tracing_subscriber::fmt::Layer::with_ansi]
pub fn with_ansi(self, with_ansi: bool) -> Self {
Self { with_ansi, ..self }
}