feat(observability): logging JSON estructurado con structlog y trace_id

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Juan
2026-05-10 10:16:07 +02:00
co-authored by Claude Opus 4.7
parent 60c2bff322
commit 55d65b68bf
2 changed files with 73 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
"""Tests de logging estructurado."""
from __future__ import annotations
import json
import structlog
from agentforge_core.observability.logging import bind_trace_id, configure_logging
def test_configure_logging_emite_json(capsys) -> None: # type: ignore[no-untyped-def]
configure_logging(level="INFO")
log = structlog.get_logger()
log.info("hola", clave="valor")
captured = capsys.readouterr()
line = captured.out.strip().splitlines()[-1]
parsed = json.loads(line)
assert parsed["event"] == "hola"
assert parsed["clave"] == "valor"
def test_bind_trace_id_aparece_en_logs(capsys) -> None: # type: ignore[no-untyped-def]
configure_logging(level="INFO")
bind_trace_id("trace-abc-123")
log = structlog.get_logger()
log.info("evento")
line = capsys.readouterr().out.strip().splitlines()[-1]
parsed = json.loads(line)
assert parsed["trace_id"] == "trace-abc-123"