Add ability to disable colorized logs

This commit is contained in:
asonix 2024-04-15 15:16:10 -05:00
parent 3428c31f16
commit c4e99ef539
6 changed files with 31 additions and 2 deletions

View file

@ -16,6 +16,7 @@ concurrency = 32
format = "normal" format = "normal"
targets = "info" targets = "info"
log_spans = false log_spans = false
no_ansi = false
[tracing.console] [tracing.console]
buffer_capacity = 102400 buffer_capacity = 102400

View file

@ -98,6 +98,11 @@ targets = 'info'
# default: false # default: false
log_spans = false log_spans = false
## Optional: whether to disable colorized log output
# environment variable: PICTRS__TRACING__LOGGING__NO_ANSI
# default: false
no_ansi = false
## Console configuration ## Console configuration
[tracing.console] [tracing.console]

View file

@ -18,6 +18,7 @@ impl Args {
log_format, log_format,
log_targets, log_targets,
log_spans, log_spans,
no_log_ansi,
console_address, console_address,
console_buffer_capacity, console_buffer_capacity,
opentelemetry_url, opentelemetry_url,
@ -38,6 +39,7 @@ impl Args {
format: log_format, format: log_format,
targets: log_targets.map(Serde::new), targets: log_targets.map(Serde::new),
log_spans, log_spans,
no_ansi: no_log_ansi,
}, },
console: Console { console: Console {
address: console_address, address: console_address,
@ -581,6 +583,8 @@ struct Logging {
targets: Option<Serde<Targets>>, targets: Option<Serde<Targets>>,
#[serde(skip_serializing_if = "std::ops::Not::not")] #[serde(skip_serializing_if = "std::ops::Not::not")]
log_spans: bool, log_spans: bool,
#[serde(skip_serializing_if = "std::ops::Not::not")]
no_ansi: bool,
} }
#[derive(Debug, Default, serde::Serialize)] #[derive(Debug, Default, serde::Serialize)]
@ -925,6 +929,10 @@ pub(super) struct Args {
#[arg(long)] #[arg(long)]
log_spans: bool, log_spans: bool,
#[arg(long)]
/// Whether to disable color-codes in log output
no_log_ansi: bool,
/// Address and port to expose tokio-console metrics /// Address and port to expose tokio-console metrics
#[arg(long)] #[arg(long)]
console_address: Option<SocketAddr>, console_address: Option<SocketAddr>,

View file

@ -55,6 +55,7 @@ struct LoggingDefaults {
format: LogFormat, format: LogFormat,
targets: Serde<Targets>, targets: Serde<Targets>,
log_spans: bool, log_spans: bool,
no_ansi: bool,
} }
#[derive(Clone, Debug, serde::Serialize)] #[derive(Clone, Debug, serde::Serialize)]
@ -235,6 +236,7 @@ impl Default for LoggingDefaults {
format: LogFormat::Normal, format: LogFormat::Normal,
targets: "info".parse().expect("Valid targets string"), targets: "info".parse().expect("Valid targets string"),
log_spans: false, log_spans: false,
no_ansi: false,
} }
} }
} }

View file

@ -163,6 +163,8 @@ pub(crate) struct Logging {
pub(crate) targets: Serde<Targets>, pub(crate) targets: Serde<Targets>,
pub(crate) log_spans: bool, pub(crate) log_spans: bool,
pub(crate) no_ansi: bool,
} }
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]

View file

@ -1,4 +1,5 @@
use crate::config::{LogFormat, OpenTelemetry, Tracing}; use crate::config::{LogFormat, OpenTelemetry, Tracing};
use color_eyre::config::Theme;
use console_subscriber::ConsoleLayer; use console_subscriber::ConsoleLayer;
use opentelemetry::KeyValue; use opentelemetry::KeyValue;
use opentelemetry_otlp::WithExportConfig; use opentelemetry_otlp::WithExportConfig;
@ -11,7 +12,15 @@ use tracing_subscriber::{
}; };
pub(super) fn init_tracing(tracing: &Tracing) -> color_eyre::Result<()> { pub(super) fn init_tracing(tracing: &Tracing) -> color_eyre::Result<()> {
color_eyre::install()?; let eyre_theme = if tracing.logging.no_ansi {
Theme::new()
} else {
Theme::dark()
};
color_eyre::config::HookBuilder::new()
.theme(eyre_theme)
.install()?;
LogTracer::init()?; LogTracer::init()?;
@ -23,7 +32,9 @@ pub(super) fn init_tracing(tracing: &Tracing) -> color_eyre::Result<()> {
FmtSpan::NONE FmtSpan::NONE
}; };
let format_layer = tracing_subscriber::fmt::layer().with_span_events(fmt_span); let format_layer = tracing_subscriber::fmt::layer()
.with_span_events(fmt_span)
.with_ansi(!tracing.logging.no_ansi);
match tracing.logging.format { match tracing.logging.format {
LogFormat::Compact => with_format(format_layer.compact(), tracing), LogFormat::Compact => with_format(format_layer.compact(), tracing),