From fcee4265c605c09062d7cb2984c2776e8b8f0247 Mon Sep 17 00:00:00 2001 From: tokiedokie Date: Fri, 4 Sep 2020 05:16:49 +0900 Subject: [PATCH] support env_logger / RUST_LOG (#7142) --- Cargo.lock | 23 ++++++++++++++ cli/Cargo.toml | 1 + cli/main.rs | 56 ++++++++++++++-------------------- cli/tests/integration_tests.rs | 30 ++++++++++++++++++ 4 files changed, 77 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d51e476bf0..0ff3e3a33e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -388,6 +388,7 @@ dependencies = [ "dlopen", "dprint-plugin-typescript", "encoding_rs", + "env_logger", "filetime", "futures", "fwdansi", @@ -586,6 +587,19 @@ dependencies = [ "syn 1.0.36", ] +[[package]] +name = "env_logger" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +dependencies = [ + "atty", + "humantime", + "log 0.4.11", + "regex", + "termcolor", +] + [[package]] name = "errno" version = "0.1.8" @@ -909,6 +923,15 @@ version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" +[[package]] +name = "humantime" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" +dependencies = [ + "quick-error", +] + [[package]] name = "hyper" version = "0.13.6" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 74694c1657..bcd73cfc06 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -50,6 +50,7 @@ jsonc-parser = "0.14.0" lazy_static = "1.4.0" libc = "0.2.74" log = "0.4.11" +env_logger = "0.7.1" notify = "5.0.0-pre.3" rand = "0.7.3" regex = "1.3.9" diff --git a/cli/main.rs b/cli/main.rs index d6b74d8a2a..574ba9ed2f 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -91,8 +91,6 @@ use flags::Flags; use futures::future::FutureExt; use futures::Future; use log::Level; -use log::Metadata; -use log::Record; use state::exit_unstable; use std::env; use std::io::Read; @@ -104,35 +102,6 @@ use std::sync::Arc; use upgrade::upgrade_command; use url::Url; -static LOGGER: Logger = Logger; - -// TODO(ry) Switch to env_logger or other standard crate. -struct Logger; - -impl log::Log for Logger { - fn enabled(&self, metadata: &Metadata) -> bool { - metadata.level() <= log::max_level() - } - - fn log(&self, record: &Record) { - if self.enabled(record.metadata()) { - let mut target = record.target().to_string(); - - if let Some(line_no) = record.line() { - target.push_str(":"); - target.push_str(&line_no.to_string()); - } - - if record.level() >= Level::Info { - eprintln!("{}", record.args()); - } else { - eprintln!("{} RS - {} - {}", record.level(), target, record.args()); - } - } - } - fn flush(&self) {} -} - fn write_to_stdout_ignore_sigpipe(bytes: &[u8]) -> Result<(), std::io::Error> { use std::io::ErrorKind; @@ -705,7 +674,6 @@ pub fn main() { #[cfg(windows)] colors::enable_ansi(); // For Windows 10 - log::set_logger(&LOGGER).unwrap(); let args: Vec = env::args().collect(); let flags = flags::flags_from_vec(args); @@ -737,7 +705,29 @@ pub fn main() { Some(level) => level, None => Level::Info, // Default log level }; - log::set_max_level(log_level.to_level_filter()); + env_logger::Builder::from_env( + env_logger::Env::default() + .default_filter_or(log_level.to_level_filter().to_string()), + ) + .format(|buf, record| { + let mut target = record.target().to_string(); + if let Some(line_no) = record.line() { + target.push_str(":"); + target.push_str(&line_no.to_string()); + } + if record.level() >= Level::Info { + writeln!(buf, "{}", record.args()) + } else { + writeln!( + buf, + "{} RS - {} - {}", + record.level(), + target, + record.args() + ) + } + }) + .init(); let fut = match flags.clone().subcommand { DenoSubcommand::Bundle { diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index b8be7262d2..08c68a9ae2 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -3349,3 +3349,33 @@ fn should_not_panic_on_undefined_deno_dir_and_home_environment_variables() { .unwrap(); assert!(output.status.success()); } + +#[test] +fn rust_log() { + // Without RUST_LOG the stderr is empty. + let output = util::deno_cmd() + .current_dir(util::root_path()) + .arg("run") + .arg("cli/tests/001_hello.js") + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap() + .wait_with_output() + .unwrap(); + assert!(output.status.success()); + assert!(output.stderr.is_empty()); + + // With RUST_LOG the stderr is not empty. + let output = util::deno_cmd() + .current_dir(util::root_path()) + .arg("run") + .arg("cli/tests/001_hello.js") + .env("RUST_LOG", "debug") + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap() + .wait_with_output() + .unwrap(); + assert!(output.status.success()); + assert!(!output.stderr.is_empty()); +}