Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion libby/keygrabber/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion tests/test_daemon_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading