From 3fecdd408c1eba73a21629f99c7755d822cdcd19 Mon Sep 17 00:00:00 2001 From: Yash Jagtap Date: Mon, 30 Nov 2026 17:50:36 +0530 Subject: [PATCH] feat: add a --log-level flag node, beacon and validator take --log-level . It sets the floor node_log_filter hard-coded as info. RUST_LOG still adds per-target directives on top of it, and the gossipsub cap stays unless a directive names that target. Without the flag nothing changes. Closes #821 --- bin/ethlambda/src/cli.rs | 9 +++++ bin/ethlambda/src/command.rs | 23 +++++++++++ bin/ethlambda/src/main.rs | 73 +++++++++++++++++++++++----------- bin/ethlambda/src/validator.rs | 7 ++++ docs/cli.md | 2 + 5 files changed, 91 insertions(+), 23 deletions(-) diff --git a/bin/ethlambda/src/cli.rs b/bin/ethlambda/src/cli.rs index a6e41230d..c4b7c2bf5 100644 --- a/bin/ethlambda/src/cli.rs +++ b/bin/ethlambda/src/cli.rs @@ -67,6 +67,9 @@ impl clap::builder::TypedValueParser for ClickHouseUrlParser { } } +/// The values `--log-level` takes, on `node`, `beacon` and `validator`. +pub(crate) const LOG_LEVELS: [&str; 5] = ["error", "warn", "info", "debug", "trace"]; + /// Flags every sub-command takes, with the same meaning on each. /// /// `--node-key` and `--checkpoint-sync-url` used to be declared once per @@ -106,6 +109,12 @@ pub(crate) struct CommonOptions { pub(crate) api_port: u16, #[arg(long, default_value = "5054")] pub(crate) metrics_port: u16, + /// Lowest level to log. `info` when unset. + /// + /// `RUST_LOG` still applies on top of it, for per-target directives + /// (`RUST_LOG=ethlambda_p2p=trace`). + #[arg(long, value_name = "LEVEL", value_parser = LOG_LEVELS)] + pub(crate) log_level: Option, /// Directory for RocksDB storage #[arg(long, default_value = "./data")] pub(crate) data_dir: PathBuf, diff --git a/bin/ethlambda/src/command.rs b/bin/ethlambda/src/command.rs index 1ec8c25ad..2e790b398 100644 --- a/bin/ethlambda/src/command.rs +++ b/bin/ethlambda/src/command.rs @@ -323,6 +323,29 @@ mod tests { } } + /// `node`, `beacon` and `validator` all take `--log-level`, and only the + /// five level names. + #[test] + fn the_long_running_sub_commands_take_a_log_level() { + let validator = "validator --beacon-nodes http://x --validators-dir v --secrets-dir s"; + let node = with_node_token()[1..].join(" "); + for sub_command in [node.as_str(), "beacon", validator] { + let parse = |level| { + let args = format!("ethlambda {sub_command} --log-level {level}"); + try_parse_from(args.split(' ').map(OsString::from)) + }; + let level = match parse("debug").expect("a level name parses") { + Command::Node(options) => options.common.log_level, + Command::Beacon(options) => options.common.log_level, + Command::Validator(options) => options.log_level, + other => panic!("unexpected sub-command: {other:?}"), + }; + assert_eq!(level.as_deref(), Some("debug"), "for {sub_command}"); + let err = parse("verbose").expect_err("not a level name"); + assert_eq!(err.kind(), ErrorKind::InvalidValue, "for {sub_command}"); + } + } + /// Parse `beacon` with the given extra flags and return the custody count. fn beacon_custody_group_count(extra: &[&str]) -> Result { let mut args = vec!["ethlambda", BEACON]; diff --git a/bin/ethlambda/src/main.rs b/bin/ethlambda/src/main.rs index d0f2465e3..654745d65 100644 --- a/bin/ethlambda/src/main.rs +++ b/bin/ethlambda/src/main.rs @@ -90,11 +90,11 @@ fn main() -> eyre::Result<()> { // `Network`. The sub-command is only how the choice is spelled on the // command line. Command::Node(options) => { - init_node_logging()?; + init_node_logging(options.common.log_level.as_deref())?; run(options.into()) } Command::Beacon(options) => { - init_node_logging()?; + init_node_logging(options.common.log_level.as_deref())?; run(options.into()) } // The benchmark is synchronous, CPU-bound work, so it runs on this @@ -111,7 +111,7 @@ fn main() -> eyre::Result<()> { era::run(options) } Command::Validator(options) => { - init_node_logging()?; + init_node_logging(options.log_level.as_deref())?; validator::run(options) } // A one-shot command whose stdout may carry a pre-signed exit, so its @@ -134,7 +134,7 @@ fn main() -> eyre::Result<()> { } } -/// The level the node logs at when `RUST_LOG` says nothing. +/// The level the node logs at when `--log-level` and `RUST_LOG` say nothing. const NODE_LOG_LEVEL: &str = "info"; /// gossipsub's warnings are dropped; only its errors show. @@ -147,18 +147,23 @@ const NODE_LOG_LEVEL: &str = "info"; /// scoring handles too, so none of them is an operator's concern. const GOSSIPSUB_LOG_CAP: &str = "libp2p_gossipsub=error"; -/// Node logging: [`NODE_LOG_LEVEL`] and [`GOSSIPSUB_LOG_CAP`], amended by -/// `RUST_LOG`, on stdout. -fn init_node_logging() -> eyre::Result<()> { +/// Node logging: `--log-level` (else [`NODE_LOG_LEVEL`]) and +/// [`GOSSIPSUB_LOG_CAP`], amended by `RUST_LOG`, on stdout. +fn init_node_logging(log_level: Option<&str>) -> eyre::Result<()> { let rust_log = std::env::var(EnvFilter::DEFAULT_ENV).ok(); - let subscriber = Registry::default() - .with(tracing_subscriber::fmt::layer().with_filter(node_log_filter(rust_log.as_deref()))); + let filter = node_log_filter(log_level, rust_log.as_deref()); + let subscriber = Registry::default().with(tracing_subscriber::fmt::layer().with_filter(filter)); tracing::subscriber::set_global_default(subscriber) .wrap_err("failed to set global tracing subscriber") } -/// The node's log filter: [`GOSSIPSUB_LOG_CAP`], then the `RUST_LOG` -/// directives when there are any, else [`NODE_LOG_LEVEL`]. +/// The node's log filter: the `--log-level` floor when the flag is given, +/// [`GOSSIPSUB_LOG_CAP`], then the `RUST_LOG` directives when there are any, +/// else [`NODE_LOG_LEVEL`]. +/// +/// `--log-level` is a floor `RUST_LOG` adds to, so `--log-level debug` with +/// `RUST_LOG=ethlambda_p2p=trace` logs everything at DEBUG and that crate at +/// TRACE. Without the flag, the rules are the ones `RUST_LOG` always had. /// /// A `RUST_LOG` directive on a target the defaults name replaces the default /// (`libp2p_gossipsub=warn` brings the warnings back), one on any other target @@ -169,12 +174,12 @@ fn init_node_logging() -> eyre::Result<()> { /// rather than passed as the default directive. A bare `RUST_LOG=debug` leaves /// the cap in place, since the more specific directive wins; name the crate to /// see its chatter. -fn node_log_filter(rust_log: Option<&str>) -> EnvFilter { - let directives = match rust_log.map(str::trim).filter(|value| !value.is_empty()) { - Some(rust_log) => format!("{GOSSIPSUB_LOG_CAP},{rust_log}"), - None => format!("{NODE_LOG_LEVEL},{GOSSIPSUB_LOG_CAP}"), - }; - EnvFilter::builder().parse_lossy(directives) +fn node_log_filter(log_level: Option<&str>, rust_log: Option<&str>) -> EnvFilter { + let rust_log = rust_log.map(str::trim).filter(|value| !value.is_empty()); + let floor = log_level.or(rust_log.is_none().then_some(NODE_LOG_LEVEL)); + // An absent part is an empty directive, which the parser skips. + let directives = [floor, Some(GOSSIPSUB_LOG_CAP), rust_log].map(Option::unwrap_or_default); + EnvFilter::builder().parse_lossy(directives.join(",")) } /// Benchmark logging: WARN and above, on stderr, so that the report owns stdout @@ -2853,8 +2858,9 @@ mod tests { } /// Emits one event per (target, level) of interest under a subscriber - /// built from `node_log_filter(rust_log)` and returns what got written. - fn logged_with(rust_log: Option<&str>) -> String { + /// built from `node_log_filter(log_level, rust_log)` and returns what got + /// written. + fn logged_with(log_level: Option<&str>, rust_log: Option<&str>) -> String { use std::io::Write; use std::sync::{Arc, Mutex}; @@ -2878,7 +2884,7 @@ mod tests { tracing_subscriber::fmt::layer() .with_ansi(false) .with_writer(move || writer.clone()) - .with_filter(node_log_filter(rust_log)), + .with_filter(node_log_filter(log_level, rust_log)), ); tracing::subscriber::with_default(subscriber, || { tracing::warn!(target: "libp2p_gossipsub::behaviour", "gossipsub-warn"); @@ -2894,7 +2900,7 @@ mod tests { /// warnings, which are the send-queue-full flood; its errors still show. #[test] fn node_logging_drops_gossipsub_warnings_by_default() { - let out = logged_with(None); + let out = logged_with(None, None); assert!(out.contains("p2p-info"), "INFO floor missing: {out}"); assert!( out.contains("gossipsub-error"), @@ -2914,7 +2920,7 @@ mod tests { /// its other directives are honored as before. #[test] fn rust_log_lifts_the_gossipsub_cap_and_keeps_its_other_directives() { - let out = logged_with(Some("info,libp2p_gossipsub=warn,ethlambda_p2p=debug")); + let out = logged_with(None, Some("info,libp2p_gossipsub=warn,ethlambda_p2p=debug")); assert!(out.contains("gossipsub-warn"), "cap not lifted: {out}"); assert!(out.contains("p2p-debug"), "other directive dropped: {out}"); } @@ -2923,11 +2929,32 @@ mod tests { /// `from_env_lossy` always did, and the cap stays in place. #[test] fn rust_log_without_a_floor_still_replaces_it() { - let out = logged_with(Some("ethlambda_p2p=debug")); + let out = logged_with(None, Some("ethlambda_p2p=debug")); assert!(out.contains("p2p-debug"), "{out}"); assert!(!out.contains("gossipsub-warn"), "cap lost: {out}"); } + /// `--log-level` moves the floor in both directions, and the gossipsub + /// cap stays in place. + #[test] + fn log_level_sets_the_floor_and_keeps_the_gossipsub_cap() { + let out = logged_with(Some("debug"), None); + assert!(out.contains("p2p-debug"), "floor not lowered: {out}"); + assert!(!out.contains("gossipsub-warn"), "cap lost: {out}"); + let out = logged_with(Some("warn"), None); + assert!(!out.contains("p2p-info"), "floor not raised: {out}"); + assert!(out.contains("gossipsub-error"), "{out}"); + } + + /// `RUST_LOG` directives add to the `--log-level` floor rather than + /// replace it, and one naming gossipsub still lifts the cap. + #[test] + fn rust_log_layers_on_top_of_the_log_level_floor() { + let out = logged_with(Some("debug"), Some("libp2p_gossipsub=warn")); + assert!(out.contains("p2p-debug"), "floor replaced: {out}"); + assert!(out.contains("gossipsub-warn"), "cap not lifted: {out}"); + } + /// The duty subnet is the first explicitly assigned subnet, so an operator /// can place co-located aggregators on different subnets deliberately. #[test] diff --git a/bin/ethlambda/src/validator.rs b/bin/ethlambda/src/validator.rs index dc3c7d8c4..a3e723313 100644 --- a/bin/ethlambda/src/validator.rs +++ b/bin/ethlambda/src/validator.rs @@ -102,6 +102,12 @@ pub(crate) struct ValidatorOptions { #[arg(long, default_value = "5064")] pub(crate) metrics_port: u16, + /// Lowest level to log. `info` when unset. + /// + /// `RUST_LOG` still applies on top of it, for per-target directives. + #[arg(long, value_name = "LEVEL", value_parser = crate::cli::LOG_LEVELS)] + pub(crate) log_level: Option, + /// Text to put in the graffiti field of every block this client proposes, /// unless the keymanager API gives a validator its own. Cannot be given /// with `--graffiti-file`, as in Lighthouse. @@ -665,6 +671,7 @@ mod tests { keymanager_port: 5062, keymanager_token_file: None, metrics_port: 5064, + log_level: None, graffiti: Some(graffiti.to_string()), graffiti_file: None, suggested_fee_recipient: None, diff --git a/docs/cli.md b/docs/cli.md index 5f776abd2..4e22c4027 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -61,6 +61,7 @@ one `CommonOptions` struct flattened into each. | `--http-address` | `127.0.0.1` | Bind address for both HTTP servers | | `--api-port` | `5052` | API server port. `beacon` binds it too, off its own anchored store rather than an empty one; the lean-shaped `/lean/v0` routes still don't answer for it (see [`beacon` flags](#beacon-flags) below) | | `--metrics-port` | `5054` | Metrics and debug server port. Equal to `--api-port` merges the routers onto one listener | +| `--log-level` | `info` | Lowest level to log: `error`, `warn`, `info`, `debug` or `trace`. `RUST_LOG` still applies on top of it for per-target directives (`RUST_LOG=ethlambda_p2p=trace`), and `libp2p_gossipsub` stays capped at `error` unless a directive names it | | `--node-key` | `/node.key` | Hex file holding the secp256k1 key that is this node's libp2p and discv5 identity. When omitted, the key stored in `--data-dir` as `node.key` is used, generated and stored there (mode `0600`) on the directory's first start, so the PeerId and ENR stay the same across restarts until the directory is wiped | | `--checkpoint-sync-url` | see below | API base URLs, tried in order until one answers. Supplies `node`'s starting state on the lean API, and `beacon`'s anchor on a standard Beacon API; required on a fresh `beacon` data directory only for a built-in network, since a loaded network anchors at its own `genesis.ssz` instead. See [`checkpoint_sync.md`](checkpoint_sync.md) | | `--discovery.port` | `9000` | discv5 UDP port; must differ from `--gossipsub-port`. See [Peer discovery](./discovery.md) | @@ -508,6 +509,7 @@ slot's end, so a node serving a wrong one cannot park a duty past its slot. | `--secrets-dir` | required | Directory holding one password file per keystore, named after the validator's public key | | `--http-address` | `127.0.0.1` | Bind address for the metrics and keymanager servers | | `--metrics-port` | `5064` | Prometheus metrics port | +| `--log-level` | `info` | Lowest level to log: `error`, `warn`, `info`, `debug` or `trace`. `RUST_LOG` still applies on top of it for per-target directives (`RUST_LOG=ethlambda_p2p=trace`), and `libp2p_gossipsub` stays capped at `error` unless a directive names it | | `--keymanager-port` | `5062` | Keymanager API port. Only bound with `--enable-keymanager` | | `--suggested-fee-recipient` | none | Execution address to receive block rewards, `0x`-prefixed, for every validator the keymanager API gives no address of its own. Optional, and startup warns when it is absent: without it the beacon node pays its own fallback address (on ethlambda, `beacon --fallback-fee-recipient`), which is the zero address unless its operator set one, and may not be yours. The addresses reach each beacon node as one `prepare_beacon_proposer` request every slot, listing each validator that has an index on chain and an address, as Lighthouse's validator client sends it. The zero address is accepted, as every other client accepts it, but reported at error at startup: it burns the rewards, and builder registrations and proposer preferences sign it for the builder to pay | | `--gas-limit` | the network's schedule, else `60000000` | The gas limit every validator targets, unless the keymanager API gives it its own. Before gloas it goes into builder registrations (with `--builder-proposals`). From gloas it goes into the proposer preferences, which are published for every proposal, and the beacon node builds this client's own payloads toward it as well. Without it, each validator targets the gas limit the network's `GAS_LIMIT_SCHEDULE` (served on the beacon node's `/eth/v1/config/spec`) names for the proposal's epoch, and `60000000` for an epoch the schedule names none or a node that does not serve it, as Lighthouse does; Sepolia's and Platåberget's schedules name `200000000`, so neither needs the flag. Must be positive |