From 0e4365bd4b93422f240aa0aa39b5cdc085e564d0 Mon Sep 17 00:00:00 2001 From: Jacob Magar Date: Fri, 13 Mar 2026 10:39:49 -0400 Subject: [PATCH] fix: logging.py - no closed stream restore, attach root logger to file handler - Don't restore old_stream reference after closing it; use None/raise on failure - Attach root logger to shared file handler so library logs reach the log file Resolves review threads PRRT_kwDOO6Hdxs50E50K PRRT_kwDOO6Hdxs50E2iQ PRRT_kwDOO6Hdxs50E2iT --- unraid_mcp/config/logging.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/unraid_mcp/config/logging.py b/unraid_mcp/config/logging.py index e291ba4..6b8036a 100644 --- a/unraid_mcp/config/logging.py +++ b/unraid_mcp/config/logging.py @@ -66,7 +66,16 @@ class OverwriteFileHandler(logging.FileHandler): try: self.stream = self._open() except OSError: - self.stream = old_stream # Last resort: restore original + # old_stream is already closed — do NOT restore it. + # Leave self.stream = None so super().emit() skips output + # rather than writing to a closed file descriptor. + import sys + + print( + "WARNING: Failed to reopen log file after rotation. " + "File logging suspended until next successful open.", + file=sys.stderr, + ) if self.stream is not None: reset_record = logging.LogRecord( @@ -185,8 +194,13 @@ def configure_fastmcp_logger_with_rich() -> logging.Logger | None: fastmcp_logger.setLevel(numeric_log_level) - # Set root logger level to avoid suppressing library warnings entirely - logging.getLogger().setLevel(numeric_log_level) + # Attach shared file handler to the root logger so that library/third-party + # loggers (httpx, websockets, etc.) whose propagate=True flows up to root + # will also be written to the log file, not just the console. + root_logger = logging.getLogger() + root_logger.setLevel(numeric_log_level) + if _shared_file_handler not in root_logger.handlers: + root_logger.addHandler(_shared_file_handler) return fastmcp_logger