Configurable logging for the API and worker (SCR-5)
CI / notify (push) Successful in 6s
CI / lint (push) Successful in 18s
CI / test (push) Successful in 5m26s
CI / validate-monitoring (push) Successful in 15s
CI / federation-interop (push) Successful in 1m17s
CI / images (push) Successful in 33s
CI / assets (push) Successful in 9s
CI / python-package (push) Successful in 18s
CI / trigger-deploy (push) Successful in 3s
CI / notify (push) Successful in 6s
CI / lint (push) Successful in 18s
CI / test (push) Successful in 5m26s
CI / validate-monitoring (push) Successful in 15s
CI / federation-interop (push) Successful in 1m17s
CI / images (push) Successful in 33s
CI / assets (push) Successful in 9s
CI / python-package (push) Successful in 18s
CI / trigger-deploy (push) Successful in 3s
Shared LOG_LEVEL/LOG_FORMAT (text or json) set up in create_app, per-task activity and duration logs in the worker, and more context on delivery, inbox and import failures. Alembic's fileConfig no longer disables loggers. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
65cd610468
commit
ad383b602e
16 files changed
+212
-7
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import json
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
|
||||
from scrobbler.logsetup import JsonFormatter, configure_logging
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def restore_root_logger():
|
||||
root = logging.getLogger()
|
||||
handlers, level = list(root.handlers), root.level
|
||||
yield
|
||||
root.handlers[:] = handlers
|
||||
root.setLevel(level)
|
||||
|
||||
|
||||
def _record(msg="hello %s", args=("world",), **extra):
|
||||
record = logging.LogRecord("test.logger", logging.INFO, __file__, 1, msg, args, None)
|
||||
record.__dict__.update(extra)
|
||||
return record
|
||||
|
||||
|
||||
def test_json_formatter_emits_fields_and_extras():
|
||||
entry = json.loads(JsonFormatter().format(_record(task="art", duration_ms=12.5)))
|
||||
assert entry["message"] == "hello world"
|
||||
assert entry["level"] == "INFO"
|
||||
assert entry["logger"] == "test.logger"
|
||||
assert entry["task"] == "art"
|
||||
assert entry["duration_ms"] == 12.5
|
||||
assert "time" in entry
|
||||
|
||||
|
||||
def test_json_formatter_includes_exceptions():
|
||||
try:
|
||||
raise ValueError("boom")
|
||||
except ValueError:
|
||||
record = _record()
|
||||
import sys
|
||||
|
||||
record.exc_info = sys.exc_info()
|
||||
assert "ValueError: boom" in json.loads(JsonFormatter().format(record))["exception"]
|
||||
|
||||
|
||||
def test_configure_logging_is_idempotent_and_sets_level():
|
||||
root = logging.getLogger()
|
||||
configure_logging("DEBUG", "json")
|
||||
configure_logging("warning", "text")
|
||||
ours = [h for h in root.handlers if getattr(h, "_scrobbler_handler", False)]
|
||||
assert len(ours) == 1
|
||||
assert root.level == logging.WARNING
|
||||
|
||||
|
||||
def test_unknown_level_falls_back_to_info():
|
||||
configure_logging("LOUD", "text")
|
||||
assert logging.getLogger().level == logging.INFO
|
||||
@@ -89,3 +89,32 @@ def test_worker_cli_lists_and_runs_tasks(app, tasks):
|
||||
assert ran == [1]
|
||||
alias = app.test_cli_runner().invoke(args=["imports", "worker", "--once", "--no-metrics"])
|
||||
assert alias.exit_code == 0, alias.output
|
||||
|
||||
|
||||
def test_a_pass_that_did_work_is_logged_with_its_duration(tasks, caplog):
|
||||
tasks.register_task("busy", lambda: True)
|
||||
with caplog.at_level("INFO", logger="scrobbler.worker"):
|
||||
worker.run_once()
|
||||
record = next(r for r in caplog.records if r.__dict__.get("task") == "busy")
|
||||
assert record.levelname == "INFO"
|
||||
assert record.outcome == "worked"
|
||||
assert record.duration_ms >= 0
|
||||
|
||||
|
||||
def test_idle_passes_are_only_logged_at_debug(tasks, caplog):
|
||||
tasks.register_task("idle", lambda: False)
|
||||
with caplog.at_level("INFO", logger="scrobbler.worker"):
|
||||
worker.run_once()
|
||||
assert not [r for r in caplog.records if r.__dict__.get("task") == "idle"]
|
||||
|
||||
|
||||
def test_failures_log_the_task_name_and_duration(tasks, caplog):
|
||||
def boom():
|
||||
raise RuntimeError("nope")
|
||||
|
||||
tasks.register_task("broken", boom)
|
||||
with caplog.at_level("ERROR", logger="scrobbler.worker"):
|
||||
worker.run_once()
|
||||
record = next(r for r in caplog.records if r.__dict__.get("task") == "broken")
|
||||
assert record.outcome == "error"
|
||||
assert "broken" in record.getMessage()
|
||||
Reference in new issue
Block a user