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
9 changes: 6 additions & 3 deletions headroom/proxy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,6 @@

_MULTI_WORKER_CONFIG_ENV = "HEADROOM_PROXY_CONFIG_JSON"

# Always-on file logging to ~/.headroom/logs/ for `headroom perf` analysis
_setup_file_logging()


# Compression pipeline timeout in seconds

Expand Down Expand Up @@ -1064,6 +1061,12 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:

from contextlib import asynccontextmanager

# Always-on file logging to ~/.headroom/logs/ for `headroom perf` analysis.
# Installed here (not at module import) so importing headroom.proxy.server
# in tests or library contexts does not silently attach a RotatingFileHandler
# to the user's live proxy.log.
_setup_file_logging()

config = config or ProxyConfig()
proxy = HeadroomProxy(config)

Expand Down
33 changes: 33 additions & 0 deletions tests/test_pr208_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,39 @@ def _bad_log_dir():
# Should not raise
_setup_file_logging()

def test_importing_server_does_not_install_file_handler(self, tmp_path: Path) -> None:
"""Importing headroom.proxy.server must NOT attach a RotatingFileHandler
to the user's live proxy.log. The handler is installed by create_app()
instead, so test runs and library imports do not pollute logs.
"""
import os
import subprocess
import sys
import textwrap

script = textwrap.dedent(
"""
import logging
from logging.handlers import RotatingFileHandler
import headroom.proxy.server # noqa: F401
hr = logging.getLogger("headroom")
installed = any(isinstance(h, RotatingFileHandler) for h in hr.handlers)
print("INSTALLED" if installed else "CLEAN")
"""
).strip()
env = {**os.environ, "HEADROOM_WORKSPACE_DIR": str(tmp_path)}
result = subprocess.run(
[sys.executable, "-c", script],
capture_output=True,
text=True,
env=env,
check=True,
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
)
assert result.stdout.strip() == "CLEAN", (
f"Importing headroom.proxy.server attached a file handler: {result.stdout!r}"
)


# ---------------------------------------------------------------------------
# Repro script URL helpers and stats tests
Expand Down
Loading