diff --git a/rust/src/logger.rs b/rust/src/logger.rs index 2ddc8803f7bb6..bf567a619a084 100644 --- a/rust/src/logger.rs +++ b/rust/src/logger.rs @@ -15,17 +15,18 @@ // specific language governing permissions and limitations // under the License. -use crate::config::BooleanKey; +use crate::config::{BooleanKey, StringKey}; use crate::metadata::now_unix_timestamp; use env_logger::Target::Stdout; use env_logger::DEFAULT_FILTER_ENV; -use log::Level; use log::LevelFilter::{Debug, Info, Trace}; +use log::{Level, LevelFilter}; use serde::{Deserialize, Serialize}; use std::cell::RefCell; use std::env; use std::fmt::Display; use std::ops::Deref; +use std::str::FromStr; pub const DRIVER_PATH: &str = "Driver path: "; pub const BROWSER_PATH: &str = "Browser path: "; @@ -71,10 +72,11 @@ impl Logger { pub fn new() -> Self { let debug = BooleanKey("debug", false).get_value(); let trace = BooleanKey("trace", false).get_value(); - Logger::create("", debug, trace) + let log_level = StringKey(vec!["log-level"], "").get_value(); + Logger::create("", debug, trace, &log_level) } - pub fn create(output: &str, debug: bool, trace: bool) -> Self { + pub fn create(output: &str, debug: bool, trace: bool, log_level: &str) -> Self { let output_type; if output.eq_ignore_ascii_case("json") { output_type = OutputType::Json; @@ -86,13 +88,18 @@ impl Logger { match output_type { OutputType::Logger => { if env::var(DEFAULT_FILTER_ENV).unwrap_or_default().is_empty() { - let mut filter = match debug { - true => Debug, - false => Info, + let filter = if !log_level.is_empty() { + LevelFilter::from_str(log_level).unwrap_or(Info) + } else { + let mut filter = match debug { + true => Debug, + false => Info, + }; + if trace { + filter = Trace; + } + filter }; - if trace { - filter = Trace - } env_logger::Builder::new() .filter_module(env!("CARGO_CRATE_NAME"), filter) .target(Stdout) diff --git a/rust/src/main.rs b/rust/src/main.rs index ac044b4e8646a..d53060fae4bc4 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -122,6 +122,10 @@ struct Cli { #[clap(long)] trace: bool, + /// Level for output messages. The possible values are: info, debug, trace, warn, error + #[clap(long)] + log_level: Option, + /// Offline mode (i.e., disabling network requests and downloads) #[clap(long)] offline: bool, @@ -151,7 +155,8 @@ fn main() { let debug = cli.debug || BooleanKey("debug", false).get_value(); let trace = cli.trace || BooleanKey("trace", false).get_value(); - let log = Logger::create(&cli.output, debug, trace); + let log_level = StringKey(vec!["log-level"], &cli.log_level.unwrap_or_default()).get_value(); + let log = Logger::create(&cli.output, debug, trace, &log_level); let grid = cli.grid; let mut browser_name: String = cli.browser.unwrap_or_default(); let mut driver_name: String = cli.driver.unwrap_or_default();