2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-05-11 00:54:10 -04:00
|
|
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
struct CliLogger(env_logger::Logger);
|
|
|
|
|
|
|
|
impl CliLogger {
|
|
|
|
pub fn new(logger: env_logger::Logger) -> Self {
|
|
|
|
Self(logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn filter(&self) -> log::LevelFilter {
|
|
|
|
self.0.filter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl log::Log for CliLogger {
|
|
|
|
fn enabled(&self, metadata: &log::Metadata) -> bool {
|
2021-12-15 13:23:43 -05:00
|
|
|
self.0.enabled(metadata)
|
2021-05-11 00:54:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn log(&self, record: &log::Record) {
|
|
|
|
if self.enabled(record.metadata()) {
|
|
|
|
self.0.log(record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&self) {
|
|
|
|
self.0.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn init(maybe_level: Option<log::Level>) {
|
2021-05-11 00:54:10 -04:00
|
|
|
let log_level = maybe_level.unwrap_or(log::Level::Info);
|
|
|
|
let logger = env_logger::Builder::from_env(
|
|
|
|
env_logger::Env::default()
|
|
|
|
.default_filter_or(log_level.to_level_filter().to_string()),
|
|
|
|
)
|
|
|
|
// https://github.com/denoland/deno/issues/6641
|
|
|
|
.filter_module("rustyline", log::LevelFilter::Off)
|
|
|
|
// wgpu crates (gfx_backend), have a lot of useless INFO and WARN logs
|
|
|
|
.filter_module("wgpu", log::LevelFilter::Error)
|
|
|
|
.filter_module("gfx", log::LevelFilter::Error)
|
2024-07-04 20:41:01 -04:00
|
|
|
.filter_module("globset", log::LevelFilter::Error)
|
2021-05-11 00:54:10 -04:00
|
|
|
// used to make available the lsp_debug which is then filtered out at runtime
|
|
|
|
// in the cli logger
|
|
|
|
.filter_module("deno::lsp::performance", log::LevelFilter::Debug)
|
2023-07-17 18:49:03 -04:00
|
|
|
.filter_module("rustls", log::LevelFilter::Off)
|
2021-05-11 00:54:10 -04:00
|
|
|
.format(|buf, record| {
|
|
|
|
let mut target = record.target().to_string();
|
|
|
|
if let Some(line_no) = record.line() {
|
|
|
|
target.push(':');
|
|
|
|
target.push_str(&line_no.to_string());
|
|
|
|
}
|
|
|
|
if record.level() <= log::Level::Info
|
|
|
|
|| (record.target() == "deno::lsp::performance"
|
|
|
|
&& record.level() == log::Level::Debug)
|
|
|
|
{
|
|
|
|
// Print ERROR, WARN, INFO and lsp_debug logs as they are
|
|
|
|
writeln!(buf, "{}", record.args())
|
|
|
|
} else {
|
|
|
|
// Add prefix to DEBUG or TRACE logs
|
|
|
|
writeln!(
|
|
|
|
buf,
|
|
|
|
"{} RS - {} - {}",
|
|
|
|
record.level(),
|
|
|
|
target,
|
|
|
|
record.args()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let cli_logger = CliLogger::new(logger);
|
|
|
|
let max_level = cli_logger.filter();
|
|
|
|
let r = log::set_boxed_logger(Box::new(cli_logger));
|
|
|
|
if r.is_ok() {
|
|
|
|
log::set_max_level(max_level);
|
|
|
|
}
|
|
|
|
r.expect("Could not install logger.");
|
|
|
|
}
|