diff --git a/src/bin/ant-devnet/cli.rs b/src/bin/ant-devnet/cli.rs index 8169351f..ae1e0178 100644 --- a/src/bin/ant-devnet/cli.rs +++ b/src/bin/ant-devnet/cli.rs @@ -44,7 +44,16 @@ pub struct Cli { #[arg(long)] pub manifest: Option, + /// Enable logging output. + /// When omitted, the tracing subscriber is not installed and no log + /// records are emitted, even if the binary was built with the + /// `logging` feature. `--log-level` is ignored unless this flag is set. + #[cfg(feature = "logging")] + #[arg(long, env = "ANT_ENABLE_LOGGING")] + pub enable_logging: bool, + /// Log level for devnet process. + #[cfg(feature = "logging")] #[arg(long, default_value = "info")] pub log_level: String, diff --git a/src/bin/ant-devnet/main.rs b/src/bin/ant-devnet/main.rs index d8c9fc87..1117f7de 100644 --- a/src/bin/ant-devnet/main.rs +++ b/src/bin/ant-devnet/main.rs @@ -15,7 +15,7 @@ async fn main() -> color_eyre::Result<()> { let cli = Cli::parse(); #[cfg(feature = "logging")] - { + if cli.enable_logging { use tracing_subscriber::{fmt, prelude::*, EnvFilter}; let filter = diff --git a/src/bin/ant-node/cli.rs b/src/bin/ant-node/cli.rs index ddbd00b4..8eb9fa51 100644 --- a/src/bin/ant-node/cli.rs +++ b/src/bin/ant-node/cli.rs @@ -12,6 +12,7 @@ use std::path::PathBuf; #[derive(Parser, Debug)] #[command(name = "ant-node")] #[command(author, version, about, long_about = None)] +#[allow(clippy::struct_excessive_bools)] // unrelated CLI toggles, not a state machine pub struct Cli { /// Root directory for node data. #[arg(long, env = "ANT_ROOT_DIR")] @@ -61,6 +62,15 @@ pub struct Cli { #[arg(long, default_value = "9100", env = "ANT_METRICS_PORT")] pub metrics_port: u16, + /// Enable logging output. + /// When omitted, the tracing subscriber is not installed and no log + /// records are emitted, even if the binary was built with the + /// `logging` feature. The remaining `--log-*` options are ignored + /// unless this flag is set. + #[cfg(feature = "logging")] + #[arg(long, env = "ANT_ENABLE_LOGGING")] + pub enable_logging: bool, + /// Log level. #[cfg(feature = "logging")] #[arg(long, value_enum, default_value = "info", env = "RUST_LOG")] diff --git a/src/bin/ant-node/main.rs b/src/bin/ant-node/main.rs index 30560e50..3af62dc9 100644 --- a/src/bin/ant-node/main.rs +++ b/src/bin/ant-node/main.rs @@ -9,18 +9,26 @@ use ant_node::config::BootstrapSource; use ant_node::NodeBuilder; use clap::Parser; use cli::Cli; +#[cfg(feature = "logging")] +use cli::CliLogFormat; +#[cfg(feature = "logging")] +use tracing_subscriber::prelude::*; +#[cfg(feature = "logging")] +use tracing_subscriber::{fmt, EnvFilter, Layer}; -/// Initialize the tracing subscriber when the `logging` feature is active. +/// Initialize the tracing subscriber when the `logging` feature is active +/// **and** the user passed `--enable-logging`. /// -/// Returns a guard that must be held for the lifetime of the process to ensure -/// buffered logs are flushed on shutdown. +/// Returns `Ok(None)` when logging was not requested. Otherwise returns a +/// guard (possibly `None` for stdout sinks) that must be held for the +/// lifetime of the process to ensure buffered logs are flushed on shutdown. #[cfg(feature = "logging")] fn init_logging( cli: &Cli, ) -> color_eyre::Result> { - use cli::CliLogFormat; - use tracing_subscriber::prelude::*; - use tracing_subscriber::{fmt, EnvFilter, Layer}; + if !cli.enable_logging { + return Ok(None); + } let log_format = cli.log_format; let log_dir = cli.log_dir.clone();