Replace log dict toggles with logdiff kwarg and kanta.transaction.diff logger

Per-transaction logdiff=False skips building and printing the diff body,
logging only the header. Globally, configure_logging(diff=False) disables
the kanta.transaction.diff child logger, which now carries all diff lines,
so applications can route or silence diffs separately from headers.
This commit is contained in:
Leo Vasanko
2026-08-07 01:26:50 +00:00
parent 9e19a1bf21
commit e42f81f44f
7 changed files with 132 additions and 110 deletions
+28 -15
View File
@@ -30,10 +30,17 @@ def test_colorize_header_parts_missing_user_and_extra():
@pytest.fixture(autouse=True)
def _reset_kanta_loggers():
yield
for name in ("kanta", "kanta.transaction", "kanta.bootstrap", "kanta.migration"):
for name in (
"kanta",
"kanta.transaction",
"kanta.transaction.diff",
"kanta.bootstrap",
"kanta.migration",
):
logger = logging.getLogger(name)
logger.setLevel(logging.NOTSET)
logger.propagate = True
logger.disabled = False
logger.handlers.clear()
@@ -80,30 +87,36 @@ def test_log_change_appends_extra_string(capsys):
assert f"{_TARGET}mydb.db{_RESET}" in captured.err
def test_log_change_log_diff_false(capsys):
def test_log_change_log_diff_false(capsys, monkeypatch):
kanta_logger = logging.getLogger("kanta")
kanta_logger.handlers.clear()
configure_logging()
def _boom(*args, **kwargs):
raise AssertionError("format_diff should not be called")
monkeypatch.setattr("kanta.logging.format_diff", _boom)
log_change("update", {"counter": 5}, previous={}, log_diff=False)
captured = capsys.readouterr()
assert "update" in captured.err
assert "counter" not in captured.err
def test_log_change_log_header_false(capsys):
def test_configure_logging_diff_false(capsys):
kanta_logger = logging.getLogger("kanta")
kanta_logger.handlers.clear()
configure_logging()
log_change("update", {"counter": 5}, previous={}, log_header=False)
configure_logging(diff=False)
log_change("update", {"counter": 5}, previous={})
captured = capsys.readouterr()
assert "update" in captured.err
assert "counter" not in captured.err
def test_configure_logging_diff_true_reenables(capsys):
kanta_logger = logging.getLogger("kanta")
kanta_logger.handlers.clear()
configure_logging(diff=False)
configure_logging(diff=True)
log_change("update", {"counter": 5}, previous={})
captured = capsys.readouterr()
assert "update" not in captured.err
assert "counter" in captured.err
def test_log_change_both_disabled_logs_nothing(capsys):
kanta_logger = logging.getLogger("kanta")
kanta_logger.handlers.clear()
configure_logging()
log_change("update", {"counter": 5}, previous={}, log_header=False, log_diff=False)
captured = capsys.readouterr()
assert captured.err == ""