Skip to content

Commit

Permalink
add cli option for log formatting (Closes #531) (#784)
Browse files Browse the repository at this point in the history
* add cli option for log format

* add default()

* rename
  • Loading branch information
Baschtie authored Sep 10, 2023
1 parent d653025 commit d8e93b6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
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,
}

/// 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

0 comments on commit d8e93b6

Please sign in to comment.