summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authortokiedokie <thetokiedokie@gmail.com>2020-09-04 05:16:49 +0900
committerGitHub <noreply@github.com>2020-09-03 16:16:49 -0400
commitfcee4265c605c09062d7cb2984c2776e8b8f0247 (patch)
tree4a6b909e035a7f8678f22122fc4b530d6c6caeb6 /cli
parentdbd941148c472171eacec364c689a2a50fa0653d (diff)
support env_logger / RUST_LOG (#7142)
Diffstat (limited to 'cli')
-rw-r--r--cli/Cargo.toml1
-rw-r--r--cli/main.rs56
-rw-r--r--cli/tests/integration_tests.rs30
3 files changed, 54 insertions, 33 deletions
diff --git a/cli/Cargo.toml b/cli/Cargo.toml
index 74694c165..bcd73cfc0 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 d6b74d8a2..574ba9ed2 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<String> = 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 b8be7262d..08c68a9ae 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());
+}