diff --git a/crates/core/tedge_agent/src/lib.rs b/crates/core/tedge_agent/src/lib.rs index f14e264367..63b4ed26fa 100644 --- a/crates/core/tedge_agent/src/lib.rs +++ b/crates/core/tedge_agent/src/lib.rs @@ -44,6 +44,12 @@ pub struct AgentOpt { #[clap(long)] pub debug: bool, + /// Logging level. + /// + /// One of error/warn/info/debug/trace. Takes precedence over `--debug` + #[clap(long)] + pub level: Option, + /// Start the agent with clean session off, subscribe to the topics, so that no messages are lost #[clap(short, long)] pub init: bool, @@ -72,12 +78,17 @@ pub async fn run(agent_opt: AgentOpt) -> Result<(), anyhow::Error> { let tedge_config_location = tedge_config::TEdgeConfigLocation::from_custom_root(agent_opt.config_dir.clone()); + // If `--level` was provided, use that log level. // If `debug` is `false` then only `error!`, `warn!` and `info!` are reported. // If `debug` is `true` then also `debug!` is reported. - let log_level = if agent_opt.debug { - tracing::Level::DEBUG - } else { - get_log_level("tedge-agent", &tedge_config_location.tedge_config_root_path)? + // If neither was provided, use a log level from a config file. + let log_level = agent_opt + .level + .or(agent_opt.debug.then_some(tracing::Level::DEBUG)); + + let log_level = match log_level { + Some(log_level) => log_level, + None => get_log_level("tedge-agent", &tedge_config_location.tedge_config_root_path)?, }; set_log_level(log_level); diff --git a/crates/core/tedge_mapper/src/lib.rs b/crates/core/tedge_mapper/src/lib.rs index c2b9eb4a67..f381220d1c 100644 --- a/crates/core/tedge_mapper/src/lib.rs +++ b/crates/core/tedge_mapper/src/lib.rs @@ -64,6 +64,12 @@ pub struct MapperOpt { #[clap(subcommand)] pub name: MapperName, + /// Logging level. + /// + /// One of error/warn/info/debug/trace. Takes precedence over `--debug` + #[clap(long)] + pub level: Option, + /// Turn-on the debug log level. /// /// If off only reports ERROR, WARN, and INFO @@ -122,14 +128,19 @@ pub async fn run(mapper_opt: MapperOpt) -> anyhow::Result<()> { tedge_config::TEdgeConfigLocation::from_custom_root(&mapper_opt.config_dir); let config = tedge_config::TEdgeConfig::try_new(tedge_config_location.clone())?; - let log_level = if mapper_opt.debug { - tracing::Level::DEBUG - } else { - get_log_level( - "tedge-mapper", - &tedge_config_location.tedge_config_root_path, - )? + // If `--level` was provided, use that log level. + // If `debug` is `false` then only `error!`, `warn!` and `info!` are reported. + // If `debug` is `true` then also `debug!` is reported. + // If neither was provided, use a log level from a config file. + let log_level = mapper_opt + .level + .or(mapper_opt.debug.then_some(tracing::Level::DEBUG)); + + let log_level = match log_level { + Some(log_level) => log_level, + None => get_log_level("tedge-agent", &tedge_config_location.tedge_config_root_path)?, }; + set_log_level(log_level); // Run only one instance of a mapper (if enabled) diff --git a/crates/core/tedge_watchdog/src/lib.rs b/crates/core/tedge_watchdog/src/lib.rs index ff0ba9ea77..1e25c9f691 100644 --- a/crates/core/tedge_watchdog/src/lib.rs +++ b/crates/core/tedge_watchdog/src/lib.rs @@ -23,6 +23,12 @@ version = clap::crate_version!(), about = clap::crate_description!() )] pub struct WatchdogOpt { + /// Logging level. + /// + /// One of error/warn/info/debug/trace. Takes precedence over `--debug` + #[clap(long)] + pub level: Option, + /// Turn-on the debug log level. /// /// If off only reports ERROR, WARN, and INFO @@ -46,13 +52,17 @@ pub async fn run(watchdog_opt: WatchdogOpt) -> Result<(), anyhow::Error> { let tedge_config_location = tedge_config::TEdgeConfigLocation::from_custom_root(watchdog_opt.config_dir.clone()); - let log_level = if watchdog_opt.debug { - tracing::Level::DEBUG - } else { - get_log_level( - "tedge-watchdog", - &tedge_config_location.tedge_config_root_path, - )? + // If `--level` was provided, use that log level. + // If `debug` is `false` then only `error!`, `warn!` and `info!` are reported. + // If `debug` is `true` then also `debug!` is reported. + // If neither was provided, use a log level from a config file. + let log_level = watchdog_opt + .level + .or(watchdog_opt.debug.then_some(tracing::Level::DEBUG)); + + let log_level = match log_level { + Some(log_level) => log_level, + None => get_log_level("tedge-agent", &tedge_config_location.tedge_config_root_path)?, }; set_log_level(log_level); diff --git a/plugins/c8y_firmware_plugin/src/lib.rs b/plugins/c8y_firmware_plugin/src/lib.rs index 1b19579acc..b529955287 100644 --- a/plugins/c8y_firmware_plugin/src/lib.rs +++ b/plugins/c8y_firmware_plugin/src/lib.rs @@ -39,6 +39,12 @@ about = clap::crate_description!(), after_help = AFTER_HELP_TEXT )] pub struct FirmwarePluginOpt { + /// Logging level. + /// + /// One of error/warn/info/debug/trace. Takes precedence over `--debug` + #[clap(long)] + pub level: Option, + /// Turn-on the debug log level. /// /// If off only reports ERROR, WARN, and INFO @@ -67,10 +73,18 @@ pub async fn run(firmware_plugin_opt: FirmwarePluginOpt) -> Result<(), anyhow::E // Load tedge config from the provided location let tedge_config_location = tedge_config::TEdgeConfigLocation::from_custom_root(&firmware_plugin_opt.config_dir); - let log_level = if firmware_plugin_opt.debug { - tracing::Level::DEBUG - } else { - get_log_level(PLUGIN_NAME, &tedge_config_location.tedge_config_root_path)? + + // If `--level` was provided, use that log level. + // If `debug` is `false` then only `error!`, `warn!` and `info!` are reported. + // If `debug` is `true` then also `debug!` is reported. + // If neither was provided, use a log level from a config file. + let log_level = firmware_plugin_opt + .level + .or(firmware_plugin_opt.debug.then_some(tracing::Level::DEBUG)); + + let log_level = match log_level { + Some(log_level) => log_level, + None => get_log_level("tedge-agent", &tedge_config_location.tedge_config_root_path)?, }; set_log_level(log_level);