Skip to content

Commit

Permalink
Disable logging unless the $RUST_LOG variable is set
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Jun 9, 2023
1 parent f93f083 commit ba40091
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 23 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ object = "0.30.0"
wasm-coredump-builder = { version = "0.1.11", optional = true }
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3", features = [ "env-filter", "fmt" ] }
clap-verbosity-flag = "2"
async-trait = "0.1.68"
tokio = { version = "1.28.1", features = ["macros", "rt-multi-thread"] }
once_cell = "1.17.1"
Expand Down
2 changes: 2 additions & 0 deletions lib/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ pub fn wasmer_main() {
}

fn wasmer_main_inner() -> Result<(), anyhow::Error> {
crate::logging::set_up_logging();

// We try to run wasmer with the normal arguments.
// Eg. `wasmer <SUBCOMMAND>`
// In case that fails, we fallback trying the Run subcommand directly.
Expand Down
5 changes: 0 additions & 5 deletions lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use std::{

use anyhow::{Context, Error};
use clap::Parser;
use clap_verbosity_flag::WarnLevel;
use once_cell::sync::Lazy;
use sha2::{Digest, Sha256};
use tempfile::NamedTempFile;
Expand Down Expand Up @@ -58,8 +57,6 @@ static WASMER_HOME: Lazy<PathBuf> = Lazy::new(|| {
/// The unstable `wasmer run` subcommand.
#[derive(Debug, Parser)]
pub struct Run {
#[clap(flatten)]
verbosity: clap_verbosity_flag::Verbosity<WarnLevel>,
/// The Wasmer home directory.
#[clap(long = "wasmer-dir", env = "WASMER_DIR", default_value = WASMER_HOME.as_os_str())]
wasmer_dir: PathBuf,
Expand Down Expand Up @@ -92,7 +89,6 @@ impl Run {
}

fn execute_inner(&self) -> Result<(), Error> {
crate::logging::set_up_logging(self.verbosity.log_level_filter());
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?;
Expand Down Expand Up @@ -360,7 +356,6 @@ impl Run {
};
let store = StoreOptions::default();
Ok(Run {
verbosity: clap_verbosity_flag::Verbosity::new(0, 0),
wasmer_dir: WASMER_HOME.clone(),
store,
wasi: Wasi::for_binfmt_interpreter()?,
Expand Down
20 changes: 4 additions & 16 deletions lib/cli/src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Logging functions for the debug feature.

use tracing_subscriber::{
filter::Directive, fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter,
fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter,
};

/// Subroutine to instantiate the loggers
pub fn set_up_logging(level: log::LevelFilter) {
/// Initialize logging based on the `$RUST_LOG` environment variable. Logs will
/// be disabled when `$RUST_LOG` isn't set.
pub fn set_up_logging() {
let fmt_layer = fmt::layer()
.with_target(true)
.with_span_events(fmt::format::FmtSpan::CLOSE)
Expand All @@ -15,7 +16,6 @@ pub fn set_up_logging(level: log::LevelFilter) {
.compact();

let filter_layer = EnvFilter::builder()
.with_default_directive(log_directive(level))
.from_env_lossy();

tracing_subscriber::registry()
Expand All @@ -34,15 +34,3 @@ fn should_emit_colors() -> bool {
isatty::stderr_isatty() && std::env::var_os("NO_COLOR").is_none()
}

fn log_directive(level: log::LevelFilter) -> Directive {
let tracing_level = match level {
log::LevelFilter::Off => tracing::level_filters::LevelFilter::OFF,
log::LevelFilter::Error => tracing::level_filters::LevelFilter::ERROR,
log::LevelFilter::Warn => tracing::level_filters::LevelFilter::WARN,
log::LevelFilter::Info => tracing::level_filters::LevelFilter::INFO,
log::LevelFilter::Debug => tracing::level_filters::LevelFilter::DEBUG,
log::LevelFilter::Trace => tracing::level_filters::LevelFilter::TRACE,
};

tracing_level.into()
}

0 comments on commit ba40091

Please sign in to comment.