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:
@@ -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"
|
||||
Reference in New Issue
Block a user