1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

support env_logger / RUST_LOG (#7142)

This commit is contained in:
tokiedokie 2020-09-04 05:16:49 +09:00 committed by GitHub
parent dbd941148c
commit fcee4265c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 33 deletions

23
Cargo.lock generated
View file

@ -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"

View file

@ -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"

View file

@ -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 {

View file

@ -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());
}