Skip to content

Commit

Permalink
feat(config): Allow config to control ansi encoding in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
john-z-yang committed Dec 24, 2024
1 parent 91f3c3b commit a39b376
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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");
Expand All @@ -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
Expand All @@ -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()
Expand Down
11 changes: 8 additions & 3 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,17 @@ pub struct LoggingConfig {
/// The environment to report to sentry errors to.
pub sentry_env: Option<Cow<'static, str>>,

/// 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 {
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a39b376

Please sign in to comment.