-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement LOG001: direct-logger-instantiation
- Loading branch information
1 parent
3112202
commit fa68d0a
Showing
11 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import logging | ||
|
||
logging.Logger(__name__) | ||
logging.Logger() | ||
logging.getLogger(__name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
crates/ruff/src/rules/flake8_logging/rules/direct_logger_instantiation.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast as ast; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
use crate::importer::ImportRequest; | ||
use crate::registry::AsRule; | ||
|
||
/// ## What it does | ||
/// Checks for direct instantiation of `logging.Logger`, as opposed to using | ||
/// `logging.getLogger()`. | ||
/// | ||
/// ## Why is this bad? | ||
/// The [Logger Objects] documentation states that: | ||
/// | ||
/// > Note that Loggers should NEVER be instantiated directly, but always | ||
/// > through the module-level function `logging.getLogger(name)`. | ||
/// | ||
/// If a logger is directly instantiated, it won't be added to the logger | ||
/// tree, and will bypass all configuration. Messages logged to it will | ||
/// only be sent to the "handler of last resort", skipping any filtering | ||
/// or formatting. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// import logging | ||
/// | ||
/// logger = logging.Logger(__name__) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// import logging | ||
/// | ||
/// logger = logging.getLogger(__name__) | ||
/// ``` | ||
/// | ||
/// [Logger Objects]: https://docs.python.org/3/library/logging.html#logger-objects | ||
#[violation] | ||
pub struct DirectLoggerInstantiation; | ||
|
||
impl Violation for DirectLoggerInstantiation { | ||
const AUTOFIX: AutofixKind = AutofixKind::Sometimes; | ||
|
||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Use `logging.getLogger()` to instantiate loggers") | ||
} | ||
|
||
fn autofix_title(&self) -> Option<String> { | ||
Some(format!("Replace with `logging.getLogger()`")) | ||
} | ||
} | ||
|
||
/// LOG001 | ||
pub(crate) fn direct_logger_instantiation(checker: &mut Checker, call: &ast::ExprCall) { | ||
if checker | ||
.semantic() | ||
.resolve_call_path(call.func.as_ref()) | ||
.is_some_and(|call_path| matches!(call_path.as_slice(), ["logging", "Logger"])) | ||
{ | ||
let mut diagnostic = Diagnostic::new(DirectLoggerInstantiation, call.func.range()); | ||
if checker.patch(diagnostic.kind.rule()) { | ||
diagnostic.try_set_fix(|| { | ||
let (import_edit, binding) = checker.importer().get_or_import_symbol( | ||
&ImportRequest::import("logging", "getLogger"), | ||
call.func.start(), | ||
checker.semantic(), | ||
)?; | ||
let reference_edit = Edit::range_replacement(binding, call.func.range()); | ||
Ok(Fix::suggested_edits(import_edit, [reference_edit])) | ||
}); | ||
} | ||
checker.diagnostics.push(diagnostic); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub(crate) use direct_logger_instantiation::*; | ||
pub(crate) use undocumented_warn::*; | ||
|
||
mod direct_logger_instantiation; | ||
mod undocumented_warn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
.../rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG001_LOG001.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_logging/mod.rs | ||
--- | ||
LOG001.py:3:1: LOG001 [*] Use `logging.getLogger()` to instantiate loggers | ||
| | ||
1 | import logging | ||
2 | | ||
3 | logging.Logger(__name__) | ||
| ^^^^^^^^^^^^^^ LOG001 | ||
4 | logging.Logger() | ||
5 | logging.getLogger(__name__) | ||
| | ||
= help: Replace with `logging.getLogger()` | ||
|
||
ℹ Suggested fix | ||
1 1 | import logging | ||
2 2 | | ||
3 |-logging.Logger(__name__) | ||
3 |+logging.getLogger(__name__) | ||
4 4 | logging.Logger() | ||
5 5 | logging.getLogger(__name__) | ||
|
||
LOG001.py:4:1: LOG001 [*] Use `logging.getLogger()` to instantiate loggers | ||
| | ||
3 | logging.Logger(__name__) | ||
4 | logging.Logger() | ||
| ^^^^^^^^^^^^^^ LOG001 | ||
5 | logging.getLogger(__name__) | ||
| | ||
= help: Replace with `logging.getLogger()` | ||
|
||
ℹ Suggested fix | ||
1 1 | import logging | ||
2 2 | | ||
3 3 | logging.Logger(__name__) | ||
4 |-logging.Logger() | ||
4 |+logging.getLogger() | ||
5 5 | logging.getLogger(__name__) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.