From e498e721bd4db4cf472310a3877b4ed40f83740d Mon Sep 17 00:00:00 2001 From: Romain Ruetschi Date: Mon, 21 Feb 2022 10:50:52 +0100 Subject: [PATCH 1/6] Allow overriding tracing filter with `RUST_LOG` env var --- relayer-cli/src/components.rs | 38 ++++++++++++++++++++--------------- relayer-cli/src/config.rs | 7 +++---- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/relayer-cli/src/components.rs b/relayer-cli/src/components.rs index d8847ee419..cd39c939ab 100644 --- a/relayer-cli/src/components.rs +++ b/relayer-cli/src/components.rs @@ -1,10 +1,14 @@ use abscissa_core::{Component, FrameworkError, FrameworkErrorKind}; use tracing_subscriber::{filter::EnvFilter, util::SubscriberInitExt, FmtSubscriber}; -use ibc_relayer::config::GlobalConfig; +use ibc_relayer::config::{GlobalConfig, LogLevel}; use crate::config::Error; +/// The name of the environment variable through which one can override +/// the tracing filter built in [`build_tracing_filter`]. +const HERMES_LOG_VAR: &str = "RUST_LOG"; + /// A custom component for parametrizing `tracing` in the relayer. /// Primarily used for: /// @@ -17,7 +21,7 @@ pub struct JsonTracing; impl JsonTracing { /// Creates a new [`JsonTracing`] component pub fn new(cfg: GlobalConfig) -> Result { - let filter = build_tracing_filter(cfg.log_level.to_string())?; + let filter = build_tracing_filter(cfg.log_level)?; // Note: JSON formatter is un-affected by ANSI 'color' option. Set to 'false'. let use_color = false; @@ -43,7 +47,7 @@ pub struct PrettyTracing; impl PrettyTracing { /// Creates a new [`PrettyTracing`] component pub fn new(cfg: GlobalConfig) -> Result { - let filter = build_tracing_filter(cfg.log_level.to_string())?; + let filter = build_tracing_filter(cfg.log_level)?; // Construct a tracing subscriber with the supplied filter and enable reloading. let builder = FmtSubscriber::builder() @@ -71,27 +75,29 @@ pub fn enable_ansi() -> bool { /// Builds a tracing filter based on the input `log_level`. /// Enables tracing exclusively for the relayer crates. /// Returns error if the filter failed to build. -fn build_tracing_filter(log_level: String) -> Result { - let target_crates = ["ibc_relayer", "ibc_relayer_cli"]; - - // SAFETY: unwrap() below works as long as `target_crates` is not empty. - let directive_raw = target_crates - .iter() - .map(|&c| format!("{}={}", c, log_level)) - .reduce(|a, b| format!("{},{}", a, b)) - .unwrap(); +fn build_tracing_filter(log_level: LogLevel) -> Result { + let directive = std::env::var(HERMES_LOG_VAR).unwrap_or_else(|_| { + let target_crates = ["ibc_relayer", "ibc_relayer_cli"]; + + // SAFETY: unwrap() below works as long as `target_crates` is not empty. + target_crates + .iter() + .map(|&c| format!("{}={}", c, log_level)) + .reduce(|a, b| format!("{},{}", a, b)) + .expect("non empty list of target crates") + }); // Build the filter directive - match EnvFilter::try_new(directive_raw.clone()) { + match EnvFilter::try_new(&directive) { Ok(out) => Ok(out), Err(e) => { eprintln!( "Unable to initialize Hermes from filter directive {:?}: {}", - directive_raw, e + directive, e ); - let our_err = Error::invalid_log_level(log_level, e); + Err(FrameworkErrorKind::ConfigError - .context(format!("{}", our_err)) + .context(Error::invalid_log_directive(directive, e)) .into()) } } diff --git a/relayer-cli/src/config.rs b/relayer-cli/src/config.rs index a06f16b274..05bf4c94c1 100644 --- a/relayer-cli/src/config.rs +++ b/relayer-cli/src/config.rs @@ -28,12 +28,11 @@ define_error! { ZeroChain |_| { "config file does not specify any chain" }, - InvalidLogLevel - { log_level: String, } + InvalidLogDirective + { directive: String, } [ TraceError ] |e| { - format!("config file specifies an invalid log level ('{0}'), caused by", - e.log_level) + format!("invalid log directive: {0:?}", e.directive) }, InvalidMode From 2239511f944edd4c36aaa919d0da0846b8df3750 Mon Sep 17 00:00:00 2001 From: Romain Ruetschi Date: Mon, 21 Feb 2022 10:51:39 +0100 Subject: [PATCH 2/6] Fix warning --- relayer-cli/src/commands/start.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/relayer-cli/src/commands/start.rs b/relayer-cli/src/commands/start.rs index cd8d34d98e..c40c624c5c 100644 --- a/relayer-cli/src/commands/start.rs +++ b/relayer-cli/src/commands/start.rs @@ -33,7 +33,7 @@ impl Runnable for StartCmd { let config = (*app_config()).clone(); let config = Arc::new(RwLock::new(config)); - let supervisor_handle = make_supervisor::(config.clone(), self.full_scan) + let supervisor_handle = make_supervisor::(config, self.full_scan) .unwrap_or_else(|e| { Output::error(format!("Hermes failed to start, last error: {}", e)).exit() }); From 6cb07ddaf99cf95d65499ac2add0a12708a1ac5b Mon Sep 17 00:00:00 2001 From: Romain Ruetschi Date: Mon, 21 Feb 2022 10:52:46 +0100 Subject: [PATCH 3/6] Add changelog entry --- .changelog/unreleased/features/ibc-relayer-cli/1895-rust-log.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changelog/unreleased/features/ibc-relayer-cli/1895-rust-log.md diff --git a/.changelog/unreleased/features/ibc-relayer-cli/1895-rust-log.md b/.changelog/unreleased/features/ibc-relayer-cli/1895-rust-log.md new file mode 100644 index 0000000000..337b05d53a --- /dev/null +++ b/.changelog/unreleased/features/ibc-relayer-cli/1895-rust-log.md @@ -0,0 +1,2 @@ +- Allow overriding the tracing filter with `RUST_LOG` environment variable + ([#1895](https://github.com/informalsystems/ibc-rs/issues/1895)) \ No newline at end of file From cfc8ea0d530179b09db61f38354df046485fe2b0 Mon Sep 17 00:00:00 2001 From: Romain Ruetschi Date: Mon, 21 Feb 2022 11:30:52 +0100 Subject: [PATCH 4/6] Rename `log_level` to `default_level` as the parameter is ignored if `RUST_LOG` is set Co-authored-by: Mikhail Zabaluev --- relayer-cli/src/components.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/relayer-cli/src/components.rs b/relayer-cli/src/components.rs index cd39c939ab..360fe45618 100644 --- a/relayer-cli/src/components.rs +++ b/relayer-cli/src/components.rs @@ -75,7 +75,7 @@ pub fn enable_ansi() -> bool { /// Builds a tracing filter based on the input `log_level`. /// Enables tracing exclusively for the relayer crates. /// Returns error if the filter failed to build. -fn build_tracing_filter(log_level: LogLevel) -> Result { +fn build_tracing_filter(default_level: LogLevel) -> Result { let directive = std::env::var(HERMES_LOG_VAR).unwrap_or_else(|_| { let target_crates = ["ibc_relayer", "ibc_relayer_cli"]; From aef7a87885ac58491070321fb80e67a93844b6d0 Mon Sep 17 00:00:00 2001 From: Romain Ruetschi Date: Mon, 21 Feb 2022 11:31:33 +0100 Subject: [PATCH 5/6] Better log message Co-authored-by: Mikhail Zabaluev --- relayer-cli/src/components.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/relayer-cli/src/components.rs b/relayer-cli/src/components.rs index 360fe45618..bb3506fd79 100644 --- a/relayer-cli/src/components.rs +++ b/relayer-cli/src/components.rs @@ -92,7 +92,7 @@ fn build_tracing_filter(default_level: LogLevel) -> Result Ok(out), Err(e) => { eprintln!( - "Unable to initialize Hermes from filter directive {:?}: {}", + "ERROR: unable to initialize Hermes with log filtering directive {:?}: {}", directive, e ); From 8329d856844df30651f68a25ca5c5db4addbaa2b Mon Sep 17 00:00:00 2001 From: Romain Ruetschi Date: Mon, 21 Feb 2022 11:51:20 +0100 Subject: [PATCH 6/6] Small refactor --- relayer-cli/src/components.rs | 26 ++++++++++++++++---------- relayer/src/config.rs | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/relayer-cli/src/components.rs b/relayer-cli/src/components.rs index bb3506fd79..645cc50e75 100644 --- a/relayer-cli/src/components.rs +++ b/relayer-cli/src/components.rs @@ -72,20 +72,26 @@ pub fn enable_ansi() -> bool { atty::is(atty::Stream::Stdout) && atty::is(atty::Stream::Stderr) } +/// The relayer crates targeted by the default log level. +const TARGET_CRATES: [&str; 2] = ["ibc_relayer", "ibc_relayer_cli"]; + +/// Build a tracing directive setting the log level for the relayer crates to the +/// given `log_level`. +fn default_directive(log_level: LogLevel) -> String { + use itertools::Itertools; + + TARGET_CRATES + .iter() + .map(|&c| format!("{}={}", c, log_level)) + .join(",") +} + /// Builds a tracing filter based on the input `log_level`. /// Enables tracing exclusively for the relayer crates. /// Returns error if the filter failed to build. fn build_tracing_filter(default_level: LogLevel) -> Result { - let directive = std::env::var(HERMES_LOG_VAR).unwrap_or_else(|_| { - let target_crates = ["ibc_relayer", "ibc_relayer_cli"]; - - // SAFETY: unwrap() below works as long as `target_crates` is not empty. - target_crates - .iter() - .map(|&c| format!("{}={}", c, log_level)) - .reduce(|a, b| format!("{},{}", a, b)) - .expect("non empty list of target crates") - }); + let directive = + std::env::var(HERMES_LOG_VAR).unwrap_or_else(|_| default_directive(default_level)); // Build the filter directive match EnvFilter::try_new(&directive) { diff --git a/relayer/src/config.rs b/relayer/src/config.rs index a94b8d973a..4ff8a4bd0c 100644 --- a/relayer/src/config.rs +++ b/relayer/src/config.rs @@ -265,7 +265,7 @@ impl Default for Packets { /// Log levels are wrappers over [`tracing_core::Level`]. /// /// [`tracing_core::Level`]: https://docs.rs/tracing-core/0.1.17/tracing_core/struct.Level.html -#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +#[derive(Copy, Clone, Debug, PartialEq, Deserialize, Serialize)] #[serde(rename_all = "lowercase")] pub enum LogLevel { Trace,