Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): Allow config to control ansi encoding in logs #109

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would we want JSON logging with escape codes?

.boxed(),
LogFormat::Text => subscriber.compact().boxed(),
LogFormat::Text => subscriber.compact().with_ansi(log_config.with_ansi).boxed(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another approach would be to have text (with-ansi) and plaintext (or a similar name) that doesn't feature ansi formatting.

};

let logs_subscriber = tracing_subscriber::registry()
Expand Down
Loading