Skip to content

Commit 9ef040e

Browse files
committed
test: cover logging setup branches
Signed-off-by: phernandez <paul@basicmachines.co>
1 parent 02e21f8 commit 9ef040e

2 files changed

Lines changed: 89 additions & 2 deletions

File tree

src/basic_memory/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def setup_logging(
251251
log_to_file: bool = False,
252252
log_to_stdout: bool = False,
253253
structured_context: bool = False,
254-
) -> None: # pragma: no cover
254+
) -> None:
255255
"""Configure logging with explicit settings.
256256
257257
This function provides a simple, explicit interface for configuring logging.
@@ -334,7 +334,7 @@ def _cleanup_windows_log_files(log_dir: Path, current_log_name: str) -> None:
334334
try:
335335
stale_log.unlink()
336336
except OSError:
337-
logger.debug(f"Failed to delete stale Windows log file: {stale_log}")
337+
logger.debug("Failed to delete stale Windows log file: {path}", path=stale_log)
338338

339339

340340
def parse_tags(tags: Union[List[str], str, None]) -> List[str]:

tests/utils/test_setup_logging.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for logging setup helpers."""
22

33
import os
4+
import sys
45

56
from basic_memory import utils
67

@@ -73,3 +74,89 @@ def test_setup_logging_trims_stale_windows_pid_logs(monkeypatch, tmp_path) -> No
7374
"basic-memory-1004.log",
7475
"basic-memory-1005.log",
7576
]
77+
78+
79+
def test_setup_logging_test_env_uses_stderr_only(monkeypatch) -> None:
80+
"""Test mode should add one stderr sink and return before other branches run."""
81+
added_sinks: list[object] = []
82+
configured_calls: list[dict] = []
83+
84+
monkeypatch.setenv("BASIC_MEMORY_ENV", "test")
85+
monkeypatch.setattr(utils.logger, "remove", lambda *args, **kwargs: None)
86+
monkeypatch.setattr(utils.logger, "add", lambda sink, **kwargs: added_sinks.append(sink))
87+
monkeypatch.setattr(
88+
utils.logger,
89+
"configure",
90+
lambda **kwargs: configured_calls.append(kwargs),
91+
)
92+
93+
utils.setup_logging(log_to_file=True, log_to_stdout=True, structured_context=True)
94+
95+
assert added_sinks == [sys.stderr]
96+
assert configured_calls == []
97+
98+
99+
def test_setup_logging_log_to_stdout(monkeypatch) -> None:
100+
"""stdout logging should attach a stderr sink outside test mode."""
101+
added_sinks: list[object] = []
102+
103+
monkeypatch.setenv("BASIC_MEMORY_ENV", "dev")
104+
monkeypatch.setattr(utils.logger, "remove", lambda *args, **kwargs: None)
105+
monkeypatch.setattr(utils.logger, "add", lambda sink, **kwargs: added_sinks.append(sink))
106+
107+
utils.setup_logging(log_to_stdout=True)
108+
109+
assert added_sinks == [sys.stderr]
110+
111+
112+
def test_setup_logging_structured_context(monkeypatch) -> None:
113+
"""Structured context should bind cloud metadata into loguru extras."""
114+
configured_extras: list[dict[str, str]] = []
115+
116+
monkeypatch.setenv("BASIC_MEMORY_ENV", "dev")
117+
monkeypatch.setenv("BASIC_MEMORY_TENANT_ID", "tenant-123")
118+
monkeypatch.setenv("FLY_APP_NAME", "bm-app")
119+
monkeypatch.setenv("FLY_MACHINE_ID", "machine-123")
120+
monkeypatch.setenv("FLY_REGION", "ord")
121+
monkeypatch.setattr(utils.logger, "remove", lambda *args, **kwargs: None)
122+
monkeypatch.setattr(utils.logger, "add", lambda *args, **kwargs: None)
123+
monkeypatch.setattr(
124+
utils.logger,
125+
"configure",
126+
lambda **kwargs: configured_extras.append(kwargs["extra"]),
127+
)
128+
129+
utils.setup_logging(structured_context=True)
130+
131+
assert configured_extras == [
132+
{
133+
"tenant_id": "tenant-123",
134+
"fly_app_name": "bm-app",
135+
"fly_machine_id": "machine-123",
136+
"fly_region": "ord",
137+
}
138+
]
139+
140+
141+
def test_setup_logging_suppresses_noisy_loggers(monkeypatch) -> None:
142+
"""Third-party HTTP/file-watch loggers should be raised to WARNING."""
143+
monkeypatch.setenv("BASIC_MEMORY_ENV", "dev")
144+
monkeypatch.setattr(utils.logger, "remove", lambda *args, **kwargs: None)
145+
monkeypatch.setattr(utils.logger, "add", lambda *args, **kwargs: None)
146+
147+
httpx_logger = utils.logging.getLogger("httpx")
148+
watchfiles_logger = utils.logging.getLogger("watchfiles.main")
149+
original_httpx_level = httpx_logger.level
150+
original_watchfiles_level = watchfiles_logger.level
151+
152+
try:
153+
httpx_logger.setLevel(utils.logging.DEBUG)
154+
watchfiles_logger.setLevel(utils.logging.INFO)
155+
156+
utils.setup_logging()
157+
158+
assert httpx_logger.level == utils.logging.WARNING
159+
assert watchfiles_logger.level == utils.logging.WARNING
160+
finally:
161+
httpx_logger.setLevel(original_httpx_level)
162+
watchfiles_logger.setLevel(original_watchfiles_level)

0 commit comments

Comments
 (0)