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 --log-level flag #3205

Merged
merged 3 commits into from
Oct 28, 2024
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/common/tedge_config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ test = []
anyhow = { workspace = true }
camino = { workspace = true, features = ["serde", "serde1"] }
certificate = { workspace = true, features = ["reqwest"] }
clap = { workspace = true }
doku = { workspace = true }
figment = { workspace = true, features = ["env", "toml"] }
humantime = { workspace = true }
Expand Down
59 changes: 59 additions & 0 deletions crates/common/tedge_config/src/system_services/log_config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,69 @@
use camino::Utf8Path;
use clap::Args;

use crate::system_services::SystemConfig;
use crate::system_services::SystemServiceError;
use std::io::IsTerminal;
use std::str::FromStr;

#[derive(Args, Debug, PartialEq, Eq, Clone)]
pub struct LogConfigArgs {
/// Turn-on the DEBUG log level.
///
/// If off only reports ERROR, WARN, and INFO, if on also reports DEBUG
#[clap(long, global = true)]
pub debug: bool,

/// Configures the logging level.
///
/// One of error/warn/info/debug/trace. Logs with verbosity lower or equal to the selected level
/// will be printed, i.e. warn prints ERROR and WARN logs and trace prints logs of all levels.
///
/// Overrides `--debug`
#[clap(long, global = true)]
pub log_level: Option<tracing::Level>,
}

/// Configures and enables logging taking into account flags, env variables and file config.
///
/// 1. Log config is taken from the file configuration first
/// 2. If `RUST_LOG` variable is set, it overrides file-based configuration
/// 3. If `--debug` or `--log-level` flags are set, they override previous steps
///
/// Reports all the log events sent either with the `log` crate or the `tracing`
/// crate.
pub fn log_init(
sname: &str,
flags: &LogConfigArgs,
config_dir: &Utf8Path,
) -> Result<(), SystemServiceError> {
let subscriber = tracing_subscriber::fmt()
.with_writer(std::io::stderr)
.with_ansi(std::io::stderr().is_terminal())
.with_timer(tracing_subscriber::fmt::time::UtcTime::rfc_3339());

let log_level = flags
.log_level
.or(flags.debug.then_some(tracing::Level::DEBUG));

if let Some(log_level) = log_level {
subscriber.with_max_level(log_level).init();
return Ok(());
}

if std::env::var("RUST_LOG").is_ok() {
subscriber
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
return Ok(());
}

let log_level = get_log_level(sname, config_dir)?;
subscriber.with_max_level(log_level).init();

Ok(())
}

pub fn get_log_level(
sname: &str,
config_dir: &Utf8Path,
Expand Down
26 changes: 9 additions & 17 deletions crates/core/tedge_agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use std::sync::Arc;
use agent::AgentConfig;
use camino::Utf8PathBuf;
use tedge_config::get_config_dir;
use tedge_config::system_services::get_log_level;
use tedge_config::system_services::set_log_level;
use tedge_config::system_services::log_init;
use tedge_config::system_services::LogConfigArgs;
use tracing::log::warn;

mod agent;
Expand All @@ -37,12 +37,8 @@ version = clap::crate_version!(),
about = clap::crate_description!()
)]
pub struct AgentOpt {
/// Turn-on the debug log level.
///
/// If off only reports ERROR, WARN, and INFO
/// If on also reports DEBUG
#[clap(long)]
pub debug: bool,
#[command(flatten)]
pub log_args: LogConfigArgs,

/// Start the agent with clean session off, subscribe to the topics, so that no messages are lost
#[clap(short, long)]
Expand Down Expand Up @@ -72,15 +68,11 @@ 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 `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)?
};

set_log_level(log_level);
log_init(
"tedge-agent",
&agent_opt.log_args,
&tedge_config_location.tedge_config_root_path,
)?;

let init = agent_opt.init;

Expand Down
26 changes: 9 additions & 17 deletions crates/core/tedge_mapper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use clap::Parser;
use flockfile::check_another_instance_is_not_running;
use std::fmt;
use tedge_config::get_config_dir;
use tedge_config::system_services::get_log_level;
use tedge_config::system_services::set_log_level;
use tedge_config::system_services::log_init;
use tedge_config::system_services::LogConfigArgs;
use tedge_config::PathBuf;
use tedge_config::ProfileName;
use tracing::log::warn;
Expand Down Expand Up @@ -64,12 +64,8 @@ pub struct MapperOpt {
#[clap(subcommand)]
pub name: MapperName,

/// Turn-on the debug log level.
///
/// If off only reports ERROR, WARN, and INFO
/// If on also reports DEBUG
#[clap(long, global = true)]
pub debug: bool,
#[command(flatten)]
pub log_args: LogConfigArgs,

/// Start the mapper with clean session off, subscribe to the topics, so that no messages are lost
#[clap(short, long)]
Expand Down Expand Up @@ -122,15 +118,11 @@ 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,
)?
};
set_log_level(log_level);
log_init(
"tedge-mapper",
&mapper_opt.log_args,
&tedge_config_location.tedge_config_root_path,
)?;

// Run only one instance of a mapper (if enabled)
let mut _flock = None;
Expand Down
23 changes: 7 additions & 16 deletions crates/core/tedge_watchdog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ version = clap::crate_version!(),
about = clap::crate_description!()
)]
pub struct WatchdogOpt {
/// Turn-on the debug log level.
///
/// If off only reports ERROR, WARN, and INFO
/// If on also reports DEBUG
#[clap(long)]
pub debug: bool,
#[command(flatten)]
pub log_args: LogConfigArgs,

/// Start the watchdog from custom path
///
Expand All @@ -46,16 +42,11 @@ 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,
)?
};

set_log_level(log_level);
log_init(
"tedge-watchdog",
&watchdog_opt.log_args,
&tedge_config_location.tedge_config_root_path,
)?;

watchdog::start_watchdog(watchdog_opt.config_dir).await
}
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,6 @@ mod tests {

#[tokio::test]
async fn shouldnt_process_invalid_status_transitions() {
tedge_config::system_services::set_log_level(tracing::Level::DEBUG);
let TestHandle {
operation_handler: mut sut,
ttd: _ttd,
Expand Down
1 change: 0 additions & 1 deletion crates/extensions/c8y_mapper_ext/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2628,7 +2628,6 @@ async fn mapper_doesnt_update_status_of_subworkflow_commands_3048() {

#[tokio::test]
async fn mapper_doesnt_send_duplicate_operation_status() {
tedge_config::system_services::set_log_level(tracing::Level::DEBUG);
let ttd = TempTedgeDir::new();
let test_handle = spawn_c8y_mapper_actor(&ttd, true).await;
let TestHandle {
Expand Down
23 changes: 9 additions & 14 deletions plugins/c8y_firmware_plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use tedge_api::mqtt_topics::EntityTopicId;
use tedge_api::mqtt_topics::MqttSchema;
use tedge_api::mqtt_topics::Service;
use tedge_config::get_config_dir;
use tedge_config::system_services::get_log_level;
use tedge_config::system_services::set_log_level;
use tedge_config::system_services::log_init;
use tedge_config::system_services::LogConfigArgs;
use tedge_config::ProfileName;
use tedge_config::TEdgeConfig;
use tedge_downloader_ext::DownloaderActor;
Expand Down Expand Up @@ -39,12 +39,8 @@ about = clap::crate_description!(),
after_help = AFTER_HELP_TEXT
)]
pub struct FirmwarePluginOpt {
/// Turn-on the debug log level.
///
/// If off only reports ERROR, WARN, and INFO
/// If on also reports DEBUG
#[clap(long)]
pub debug: bool,
#[command(flatten)]
pub log_args: LogConfigArgs,

/// Create required directories
#[clap(short, long)]
Expand All @@ -67,13 +63,12 @@ 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)?
};

set_log_level(log_level);
log_init(
"c8y-firmware-plugin",
&firmware_plugin_opt.log_args,
&tedge_config_location.tedge_config_root_path,
)?;

let tedge_config = tedge_config::TEdgeConfig::try_new(tedge_config_location)?;
let c8y_profile = firmware_plugin_opt.profile.as_deref();
Expand Down