after audit
This commit is contained in:
parent
e8e20637e6
commit
a5165dc3ba
80 changed files with 8489 additions and 7302 deletions
40
tests/unit/test_logging_policy.py
Normal file
40
tests/unit/test_logging_policy.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import ast
|
||||
from pathlib import Path
|
||||
|
||||
RAW_LOG_METHODS = {
|
||||
"debug",
|
||||
"info",
|
||||
"warning",
|
||||
"error",
|
||||
"exception",
|
||||
"critical",
|
||||
}
|
||||
APP_DIR = Path(__file__).resolve().parents[2] / "app"
|
||||
|
||||
|
||||
def _raw_logger_calls(path: Path) -> list[tuple[int, str]]:
|
||||
tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
|
||||
violations: list[tuple[int, str]] = []
|
||||
for node in ast.walk(tree):
|
||||
if not isinstance(node, ast.Call):
|
||||
continue
|
||||
func = node.func
|
||||
if not isinstance(func, ast.Attribute):
|
||||
continue
|
||||
if not isinstance(func.value, ast.Name) or func.value.id != "logger":
|
||||
continue
|
||||
if func.attr not in RAW_LOG_METHODS:
|
||||
continue
|
||||
violations.append((int(node.lineno), func.attr))
|
||||
return violations
|
||||
|
||||
|
||||
def test_app_runtime_uses_structured_log_only() -> None:
|
||||
violations: list[str] = []
|
||||
for path in sorted(APP_DIR.rglob("*.py")):
|
||||
for line_number, method in _raw_logger_calls(path):
|
||||
relative_path = path.relative_to(APP_DIR.parent)
|
||||
violations.append(f"{relative_path}:{line_number} logger.{method}()")
|
||||
assert not violations, "raw logger calls found:\n" + "\n".join(violations)
|
||||
Loading…
Add table
Add a link
Reference in a new issue