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

add cli option for log formatting (Closes #531) #784

Merged
merged 4 commits into from
Sep 10, 2023
Merged
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ kube.workspace = true
trust-dns-resolver = { version = "0.23.0", features = ["tokio", "tokio-rustls", "dns-over-https-rustls"] }
async-trait = "0.1.73"
nom = "7.1.3"
atty = "0.2.14"
strum = "0.25.0"
strum_macros = "0.25"

[target.'cfg(target_os = "linux")'.dependencies]
sys-info = "0.9.1"
Expand Down
45 changes: 41 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ use std::{
sync::Arc,
};

use clap::builder::TypedValueParser;
use clap::crate_version;
use tokio::{signal, sync::watch};

use crate::{admin::Mode, Config};
use strum_macros::{Display, EnumString};

pub use self::{
agent::Agent, generate_config_schema::GenerateConfigSchema, manage::Manage, proxy::Proxy,
Expand Down Expand Up @@ -68,6 +70,27 @@ pub struct Cli {
pub quiet: bool,
#[clap(subcommand)]
pub command: Commands,
#[clap(
long,
default_value_t = LogFormats::Auto,
value_parser = clap::builder::PossibleValuesParser::new(["auto", "json", "plain", "pretty"])
.map(|s| s.parse::<LogFormats>().unwrap()),
)]
pub log_format: LogFormats,
}

/// The various log format options
#[derive(Copy, Clone, PartialEq, Eq, Debug, EnumString, Display, Default)]
pub enum LogFormats {
#[strum(serialize = "auto")]
#[default]
Auto,
#[strum(serialize = "json")]
Json,
#[strum(serialize = "plain")]
Plain,
#[strum(serialize = "pretty")]
Pretty,
Baschtie marked this conversation as resolved.
Show resolved Hide resolved
}

/// The various Quilkin commands.
Expand Down Expand Up @@ -101,11 +124,22 @@ impl Cli {
let env_filter = tracing_subscriber::EnvFilter::builder()
.with_default_directive(tracing_subscriber::filter::LevelFilter::INFO.into())
.from_env_lossy();
tracing_subscriber::fmt()
.json()
let subscriber = tracing_subscriber::fmt()
.with_file(true)
.with_env_filter(env_filter)
.init();
.with_env_filter(env_filter);

match self.log_format {
LogFormats::Auto => {
if atty::isnt(atty::Stream::Stdout) {
subscriber.json().init();
} else {
subscriber.init();
}
}
LogFormats::Json => subscriber.json().init(),
LogFormats::Plain => subscriber.init(),
LogFormats::Pretty => subscriber.pretty().init(),
}
}

tracing::info!(
Expand Down Expand Up @@ -307,6 +341,7 @@ mod tests {
}),
..<_>::default()
}),
log_format: LogFormats::default(),
};

let control_plane_admin_port = crate::test_utils::available_addr().await.port();
Expand All @@ -325,6 +360,7 @@ mod tests {
path: endpoints_file.path().to_path_buf(),
},
}),
log_format: LogFormats::default(),
};

let proxy_admin_port = crate::test_utils::available_addr().await.port();
Expand All @@ -337,6 +373,7 @@ mod tests {
management_server: vec!["http://localhost:7800".parse().unwrap()],
..<_>::default()
}),
log_format: LogFormats::default(),
};

tokio::spawn(relay.drive());
Expand Down