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;
|
|
|
|
|
2024-08-23 18:07:59 -04:00
|
|
|
use super::draw_thread::DrawThread;
|
2024-11-18 04:12:48 -05:00
|
|
|
use deno_runtime::ops::otel::OtelConfig;
|
|
|
|
use deno_runtime::ops::otel::OtelConsoleConfig;
|
2024-08-23 18:07:59 -04:00
|
|
|
|
2024-11-18 04:12:48 -05:00
|
|
|
struct CliLogger {
|
|
|
|
logger: env_logger::Logger,
|
|
|
|
otel_console_config: OtelConsoleConfig,
|
|
|
|
}
|
2021-05-11 00:54:10 -04:00
|
|
|
|
|
|
|
impl CliLogger {
|
2024-11-18 04:12:48 -05:00
|
|
|
pub fn new(
|
|
|
|
logger: env_logger::Logger,
|
|
|
|
otel_console_config: OtelConsoleConfig,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
logger,
|
|
|
|
otel_console_config,
|
|
|
|
}
|
2021-05-11 00:54:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn filter(&self) -> log::LevelFilter {
|
2024-11-18 04:12:48 -05:00
|
|
|
self.logger.filter()
|
2021-05-11 00:54:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl log::Log for CliLogger {
|
|
|
|
fn enabled(&self, metadata: &log::Metadata) -> bool {
|
2024-11-18 04:12:48 -05:00
|
|
|
self.logger.enabled(metadata)
|
2021-05-11 00:54:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn log(&self, record: &log::Record) {
|
|
|
|
if self.enabled(record.metadata()) {
|
2024-08-23 18:07:59 -04:00
|
|
|
// it was considered to hold the draw thread's internal lock
|
|
|
|
// across logging, but if outputting to stderr blocks then that
|
|
|
|
// could potentially block other threads that access the draw
|
|
|
|
// thread's state
|
|
|
|
DrawThread::hide();
|
2024-11-18 04:12:48 -05:00
|
|
|
match self.otel_console_config {
|
|
|
|
OtelConsoleConfig::Ignore => {
|
|
|
|
self.logger.log(record);
|
|
|
|
}
|
|
|
|
OtelConsoleConfig::Capture => {
|
|
|
|
self.logger.log(record);
|
|
|
|
deno_runtime::ops::otel::handle_log(record);
|
|
|
|
}
|
|
|
|
OtelConsoleConfig::Replace => {
|
|
|
|
deno_runtime::ops::otel::handle_log(record);
|
|
|
|
}
|
|
|
|
}
|
2024-08-23 18:07:59 -04:00
|
|
|
DrawThread::show();
|
2021-05-11 00:54:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&self) {
|
2024-11-18 04:12:48 -05:00
|
|
|
self.logger.flush();
|
2021-05-11 00:54:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-18 04:12:48 -05:00
|
|
|
pub fn init(maybe_level: Option<log::Level>, otel_config: Option<&OtelConfig>) {
|
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(
|
2024-09-03 04:36:28 -04:00
|
|
|
env_logger::Env::new()
|
|
|
|
// Use `DENO_LOG` and `DENO_LOG_STYLE` instead of `RUST_` prefix
|
|
|
|
.filter_or("DENO_LOG", log_level.to_level_filter().to_string())
|
|
|
|
.write_style("DENO_LOG_STYLE"),
|
2021-05-11 00:54:10 -04:00
|
|
|
)
|
|
|
|
// 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)
|
2024-08-22 22:15:10 -04:00
|
|
|
// swc_ecma_codegen's `srcmap!` macro emits error-level spans only on debug
|
|
|
|
// build:
|
|
|
|
// https://github.com/swc-project/swc/blob/74d6478be1eb8cdf1df096c360c159db64b64d8a/crates/swc_ecma_codegen/src/macros.rs#L112
|
|
|
|
// We suppress them here to avoid flooding our CI logs in integration tests.
|
|
|
|
.filter_module("swc_ecma_codegen", log::LevelFilter::Off)
|
|
|
|
.filter_module("swc_ecma_transforms_optimization", log::LevelFilter::Off)
|
|
|
|
.filter_module("swc_ecma_parser", log::LevelFilter::Error)
|
|
|
|
// Suppress span lifecycle logs since they are too verbose
|
|
|
|
.filter_module("tracing::span", log::LevelFilter::Off)
|
2024-11-01 12:27:00 -04:00
|
|
|
// for deno_compile, this is too verbose
|
|
|
|
.filter_module("editpe", log::LevelFilter::Error)
|
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();
|
|
|
|
|
2024-11-18 04:12:48 -05:00
|
|
|
let cli_logger = CliLogger::new(
|
|
|
|
logger,
|
|
|
|
otel_config
|
|
|
|
.map(|c| c.console)
|
|
|
|
.unwrap_or(OtelConsoleConfig::Ignore),
|
|
|
|
);
|
2021-05-11 00:54:10 -04:00
|
|
|
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.");
|
|
|
|
}
|