diff --git a/Cargo.lock b/Cargo.lock index a8b182b0fd..b1144b6f1b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -783,15 +783,6 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" -[[package]] -name = "fern" -version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9f0c14694cbd524c8720dd69b0e3179344f04ebb5f90f2e4a440c6ea3b2f1ee" -dependencies = [ - "log", -] - [[package]] name = "filetime" version = "0.2.22" @@ -2030,7 +2021,6 @@ dependencies = [ "chrono", "clap", "colored", - "fern", "glob", "globset", "imperative", @@ -2080,6 +2070,8 @@ dependencies = [ "test-case", "thiserror", "toml", + "tracing", + "tracing-subscriber", "typed-arena", "unicode-width", "unicode_names2", diff --git a/crates/ruff/Cargo.toml b/crates/ruff/Cargo.toml index ab41dd7b27..0f2933f1dc 100644 --- a/crates/ruff/Cargo.toml +++ b/crates/ruff/Cargo.toml @@ -37,7 +37,6 @@ bitflags = { workspace = true } chrono = { workspace = true } clap = { workspace = true, features = ["derive", "string"], optional = true } colored = { workspace = true } -fern = { version = "0.6.1" } glob = { workspace = true } globset = { workspace = true } imperative = { version = "1.0.4" } @@ -71,6 +70,8 @@ strum = { workspace = true } strum_macros = { workspace = true } thiserror = { workspace = true } toml = { workspace = true } +tracing = { workspace = true } +tracing-subscriber = { workspace = true } typed-arena = { version = "2.0.2" } unicode-width = { workspace = true } unicode_names2 = { version = "0.6.0", git = "https://github.com/youknowone/unicode_names2.git", rev = "4ce16aa85cbcdd9cc830410f1a72ef9a235f2fde" } diff --git a/crates/ruff/src/logging.rs b/crates/ruff/src/logging.rs index 6851680cfb..1e20f15c74 100644 --- a/crates/ruff/src/logging.rs +++ b/crates/ruff/src/logging.rs @@ -4,16 +4,17 @@ use std::sync::Mutex; use anyhow::Result; use colored::Colorize; -use fern; -use log::Level; use once_cell::sync::Lazy; -use ruff_python_parser::{ParseError, ParseErrorType}; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::util::SubscriberInitExt; +use tracing_subscriber::EnvFilter; +use ruff_notebook::Notebook; +use ruff_python_parser::{ParseError, ParseErrorType}; use ruff_source_file::{OneIndexed, SourceCode, SourceLocation}; use crate::fs; use crate::source_kind::SourceKind; -use ruff_notebook::Notebook; pub static WARNINGS: Lazy>> = Lazy::new(Mutex::default); @@ -90,49 +91,27 @@ pub enum LogLevel { impl LogLevel { #[allow(clippy::trivially_copy_pass_by_ref)] - const fn level_filter(&self) -> log::LevelFilter { + const fn tracing_level(&self) -> tracing::Level { match self { - LogLevel::Default => log::LevelFilter::Info, - LogLevel::Verbose => log::LevelFilter::Debug, - LogLevel::Quiet => log::LevelFilter::Off, - LogLevel::Silent => log::LevelFilter::Off, + LogLevel::Default => tracing::Level::INFO, + LogLevel::Verbose => tracing::Level::DEBUG, + LogLevel::Quiet => tracing::Level::WARN, + LogLevel::Silent => tracing::Level::ERROR, } } } +/// Log level priorities: 1. `RUST_LOG=`, 2. explicit CLI log level, 3. default to info pub fn set_up_logging(level: &LogLevel) -> Result<()> { - fern::Dispatch::new() - .format(|out, message, record| match record.level() { - Level::Error => { - out.finish(format_args!( - "{}{} {}", - "error".red().bold(), - ":".bold(), - message - )); - } - Level::Warn => { - out.finish(format_args!( - "{}{} {}", - "warning".yellow().bold(), - ":".bold(), - message - )); - } - Level::Info | Level::Debug | Level::Trace => { - out.finish(format_args!( - "{}[{}][{}] {}", - chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"), - record.target(), - record.level(), - message - )); - } - }) - .level(level.level_filter()) - .level_for("globset", log::LevelFilter::Warn) - .chain(std::io::stderr()) - .apply()?; + let filter_layer = EnvFilter::try_from_default_env().unwrap_or_else(|_| { + EnvFilter::builder() + .with_default_directive(level.tracing_level().into()) + .parse_lossy("") + }); + tracing_subscriber::registry() + .with(filter_layer) + .with(tracing_subscriber::fmt::layer()) + .init(); Ok(()) } diff --git a/crates/ruff_cli/Cargo.toml b/crates/ruff_cli/Cargo.toml index 9c6674c3e9..beb2e7565e 100644 --- a/crates/ruff_cli/Cargo.toml +++ b/crates/ruff_cli/Cargo.toml @@ -64,7 +64,7 @@ shellexpand = { workspace = true } similar = { workspace = true } strum = { workspace = true, features = [] } thiserror = { workspace = true } -tracing = { workspace = true, features = ["log"] } +tracing = { workspace = true } walkdir = { version = "2.3.2" } wild = { version = "2" }