Skip to content

Commit

Permalink
Merge pull request #45 from openebs/rust-silence
Browse files Browse the repository at this point in the history
feat: add common rust log filter with silenced modules
  • Loading branch information
tiagolobocastro authored Oct 25, 2023
2 parents 1cc075a + 9e74380 commit fde912d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
4 changes: 3 additions & 1 deletion scripts/rust/linter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

set -euo pipefail

FMT_OPTS=${FMT_OPTS:-"--check"}

for d in `find -maxdepth 2 -name Cargo.toml -printf '%h\n' | grep -v "^./h2" | grep -v "git-version-macro"`; do
pushd $d
cargo-fmt --all -- --check
cargo-fmt --all -- $FMT_OPTS
popd
done
9 changes: 9 additions & 0 deletions tracing-filter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "tracing-filter"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tracing-subscriber = { version = "0.3.17", features = [ "env-filter" ] }
37 changes: 37 additions & 0 deletions tracing-filter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// The default quiet filters in addition to `RUST_LOG`.
pub const RUST_LOG_SILENCE_DEFAULTS: &str =
"actix_web=info,actix_server=info,async_nats=info,h2=info,hyper=info,tower_buffer=info,tower=info,rustls=info,reqwest=info,tokio_util=info,tokio_tungstenite=info,tungstenite=info,async_io=info,polling=info,tonic=info,want=info,mio=info";

/// Mix the `RUST_LOG` `EnvFilter` with `RUST_LOG_SILENCE`.
/// This is useful when we want to bulk-silence certain crates by default.
pub fn rust_log_add_quiet_defaults(
current: tracing_subscriber::EnvFilter,
) -> tracing_subscriber::EnvFilter {
let rust_log_silence = std::env::var("RUST_LOG_SILENCE");
let silence = match &rust_log_silence {
Ok(quiets) => quiets.as_str(),
Err(_) => RUST_LOG_SILENCE_DEFAULTS,
};

tracing_subscriber::EnvFilter::try_new(match silence.is_empty() {
true => current.to_string(),
false => format!("{current},{silence}"),
})
.unwrap()
}

/// Get the `tracing_subscriber::EnvFilter`, taken by mixing RUST_LOG
/// and RUST_LOG_SILENCE which silences unwanted traces.
/// If no default is set from env, use the provided filter.
pub fn rust_log_filter_ext(level: &str) -> tracing_subscriber::EnvFilter {
rust_log_add_quiet_defaults(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(level)),
)
}

/// Get the `tracing_subscriber::EnvFilter`, taken by mixing RUST_LOG
/// and RUST_LOG_SILENCE which silences unwanted traces.
pub fn rust_log_filter() -> tracing_subscriber::EnvFilter {
rust_log_filter_ext("info")
}

0 comments on commit fde912d

Please sign in to comment.