|
| 1 | +use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; |
| 2 | +use ruff_macros::{derive_message_formats, violation}; |
| 3 | +use ruff_python_ast as ast; |
| 4 | +use ruff_text_size::Ranged; |
| 5 | + |
| 6 | +use crate::checkers::ast::Checker; |
| 7 | +use crate::importer::ImportRequest; |
| 8 | +use crate::registry::AsRule; |
| 9 | + |
| 10 | +/// ## What it does |
| 11 | +/// Checks for direct instantiation of `logging.Logger`, as opposed to using |
| 12 | +/// `logging.getLogger()`. |
| 13 | +/// |
| 14 | +/// ## Why is this bad? |
| 15 | +/// The [Logger Objects] documentation states that: |
| 16 | +/// |
| 17 | +/// > Note that Loggers should NEVER be instantiated directly, but always |
| 18 | +/// > through the module-level function `logging.getLogger(name)`. |
| 19 | +/// |
| 20 | +/// If a logger is directly instantiated, it won't be added to the logger |
| 21 | +/// tree, and will bypass all configuration. Messages logged to it will |
| 22 | +/// only be sent to the "handler of last resort", skipping any filtering |
| 23 | +/// or formatting. |
| 24 | +/// |
| 25 | +/// ## Example |
| 26 | +/// ```python |
| 27 | +/// import logging |
| 28 | +/// |
| 29 | +/// logger = logging.Logger(__name__) |
| 30 | +/// ``` |
| 31 | +/// |
| 32 | +/// Use instead: |
| 33 | +/// ```python |
| 34 | +/// import logging |
| 35 | +/// |
| 36 | +/// logger = logging.getLogger(__name__) |
| 37 | +/// ``` |
| 38 | +/// |
| 39 | +/// [Logger Objects]: https://docs.python.org/3/library/logging.html#logger-objects |
| 40 | +#[violation] |
| 41 | +pub struct DirectLoggerInstantiation; |
| 42 | + |
| 43 | +impl Violation for DirectLoggerInstantiation { |
| 44 | + const AUTOFIX: AutofixKind = AutofixKind::Sometimes; |
| 45 | + |
| 46 | + #[derive_message_formats] |
| 47 | + fn message(&self) -> String { |
| 48 | + format!("Use `logging.getLogger()` to instantiate loggers") |
| 49 | + } |
| 50 | + |
| 51 | + fn autofix_title(&self) -> Option<String> { |
| 52 | + Some(format!("Replace with `logging.getLogger()`")) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// LOG001 |
| 57 | +pub(crate) fn direct_logger_instantiation(checker: &mut Checker, call: &ast::ExprCall) { |
| 58 | + if checker |
| 59 | + .semantic() |
| 60 | + .resolve_call_path(call.func.as_ref()) |
| 61 | + .is_some_and(|call_path| matches!(call_path.as_slice(), ["logging", "Logger"])) |
| 62 | + { |
| 63 | + let mut diagnostic = Diagnostic::new(DirectLoggerInstantiation, call.func.range()); |
| 64 | + if checker.patch(diagnostic.kind.rule()) { |
| 65 | + diagnostic.try_set_fix(|| { |
| 66 | + let (import_edit, binding) = checker.importer().get_or_import_symbol( |
| 67 | + &ImportRequest::import("logging", "getLogger"), |
| 68 | + call.func.start(), |
| 69 | + checker.semantic(), |
| 70 | + )?; |
| 71 | + let reference_edit = Edit::range_replacement(binding, call.func.range()); |
| 72 | + Ok(Fix::suggested_edits(import_edit, [reference_edit])) |
| 73 | + }); |
| 74 | + } |
| 75 | + checker.diagnostics.push(diagnostic); |
| 76 | + } |
| 77 | +} |
0 commit comments