diff --git a/libby/keygrabber/main.py b/libby/keygrabber/main.py index da3faef..3a97e51 100644 --- a/libby/keygrabber/main.py +++ b/libby/keygrabber/main.py @@ -11,6 +11,7 @@ from collections.abc import Mapping from typing import List, Optional +from .. import telemetry from ..daemon import DEFAULT_LOG_FORMAT from ..errors import LibbyError from .daemon import KeygrabberDaemon @@ -49,7 +50,9 @@ def log_to_stderr() -> None: already given the daemon its own handler, and a second one on the root logger would print every line twice. """ - logging.basicConfig(level=logging.INFO, format=DEFAULT_LOG_FORMAT) + handler = logging.StreamHandler() + handler.setFormatter(telemetry.TraceContextFormatter(DEFAULT_LOG_FORMAT)) + logging.basicConfig(level=logging.INFO, handlers=[handler]) # pika logs each connection step at INFO, which the transport's # once-a-second reconnect loop would turn into a flood during an outage logging.getLogger("pika").setLevel(logging.WARNING) diff --git a/tests/test_daemon_logging.py b/tests/test_daemon_logging.py index fe91a31..551139d 100644 --- a/tests/test_daemon_logging.py +++ b/tests/test_daemon_logging.py @@ -12,7 +12,8 @@ from typing import Any, Dict, Optional from unittest import mock -from libby.daemon import LibbyDaemon +from libby import telemetry +from libby.daemon import DEFAULT_LOG_FORMAT, LibbyDaemon from libby.keygrabber import main as keygrabber_main # lasterror is read back from the daemon's own attribute @@ -87,6 +88,14 @@ def test_leaves_a_configured_logging_section_alone(self): """Do not add a root handler that would print every line twice.""" self._run({"level": "DEBUG"}).assert_not_called() + def test_stderr_lines_carry_trace_ids(self): + """Format stderr with the formatter that appends the active trace ids.""" + with mock.patch.object(keygrabber_main.logging, "basicConfig") as basic_config: + keygrabber_main.log_to_stderr() + (handler,) = basic_config.call_args.kwargs["handlers"] + self.assertIsInstance(handler.formatter, telemetry.TraceContextFormatter) + self.assertEqual(handler.formatter._fmt, DEFAULT_LOG_FORMAT) # pylint: disable=protected-access + def test_quiets_pika_connection_chatter(self): pika = logging.getLogger("pika") level = pika.level