diff --git a/src/config.rs b/src/config.rs index bb0adf6..a6b686b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -28,6 +28,9 @@ pub struct Config { /// The log format to use pub log_format: LogFormat, + /// Enable ANSI encoding for logs + pub log_with_ansi: bool, + /// The statsd address to report metrics to. pub statsd_addr: SocketAddr, @@ -100,6 +103,7 @@ impl Default for Config { traces_sample_rate: Some(0.0), log_level: LogLevel::Debug, log_format: LogFormat::Text, + log_with_ansi: true, grpc_addr: "0.0.0.0".to_owned(), grpc_port: 50051, statsd_addr: "127.0.0.1:8126".parse().unwrap(), @@ -203,6 +207,7 @@ mod tests { assert_eq!(config.sentry_env, None); assert_eq!(config.log_level, LogLevel::Debug); assert_eq!(config.log_format, LogFormat::Text); + assert!(config.log_with_ansi); assert_eq!(config.grpc_port, 50051); assert_eq!(config.kafka_topic, "task-worker"); assert_eq!(config.db_path, "./taskbroker-inflight.sqlite"); @@ -219,6 +224,7 @@ mod tests { sentry_env: prod log_level: info log_format: json + log_with_ansi: false statsd_addr: 127.0.0.1:8126 kafka_cluster: 10.0.0.1:9092,10.0.0.2:9092 kafka_topic: error-tasks @@ -242,6 +248,7 @@ mod tests { assert_eq!(config.sentry_env, Some(Cow::Borrowed("prod"))); assert_eq!(config.log_level, LogLevel::Error); assert_eq!(config.log_format, LogFormat::Json); + assert!(!config.log_with_ansi); assert_eq!( config.kafka_cluster, "10.0.0.1:9092,10.0.0.2:9092".to_owned() diff --git a/src/logging.rs b/src/logging.rs index df902e4..e502cb4 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -64,14 +64,17 @@ pub struct LoggingConfig { /// The environment to report to sentry errors to. pub sentry_env: Option>, - /// The tracing sample rate + /// The tracing sample rate. pub traces_sample_rate: f32, /// The log level to filter logging to. pub log_level: LogLevel, - /// The log format to use + /// The log format to use. pub log_format: LogFormat, + + /// Enable ANSI encoding for formatted events. + pub with_ansi: bool, } impl LoggingConfig { @@ -82,6 +85,7 @@ impl LoggingConfig { traces_sample_rate: config.traces_sample_rate.unwrap_or(0.0), log_level: config.log_level, log_format: config.log_format, + with_ansi: config.log_with_ansi, } } } @@ -114,8 +118,9 @@ pub fn init(log_config: LoggingConfig) { .with_span_list(true) .with_file(true) .with_line_number(true) + .with_ansi(log_config.with_ansi) .boxed(), - LogFormat::Text => subscriber.compact().boxed(), + LogFormat::Text => subscriber.compact().with_ansi(log_config.with_ansi).boxed(), }; let logs_subscriber = tracing_subscriber::registry()