Fix duplicate keyword argument error in configure_logging (#2381)

Allow traceback-related kwargs to override defaults by building a dict
with defaults first, then updating with user-provided values.

Fixes #2356

Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: William Easton <strawgate@users.noreply.github.com>
This commit is contained in:
William Easton 2025-11-06 09:11:16 -06:00 committed by GitHub
commit 05ac9457b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 10 deletions

View file

@ -74,15 +74,19 @@ def configure_logging(
import mcp
import pydantic
traceback_handler = RichHandler(
console=Console(stderr=True),
show_path=False,
show_level=False,
rich_tracebacks=enable_rich_tracebacks,
tracebacks_max_frames=3,
tracebacks_suppress=[fastmcp, mcp, pydantic],
**rich_kwargs,
)
# Build traceback kwargs with defaults that can be overridden
traceback_kwargs = {
"console": Console(stderr=True),
"show_path": False,
"show_level": False,
"rich_tracebacks": enable_rich_tracebacks,
"tracebacks_max_frames": 3,
"tracebacks_suppress": [fastmcp, mcp, pydantic],
}
# Override defaults with user-provided values
traceback_kwargs.update(rich_kwargs)
traceback_handler = RichHandler(**traceback_kwargs) # type: ignore[arg-type]
traceback_handler.setFormatter(formatter)
traceback_handler.addFilter(lambda record: record.exc_info is not None)

View file

@ -1,6 +1,6 @@
import logging
from fastmcp.utilities.logging import get_logger
from fastmcp.utilities.logging import configure_logging, get_logger
def test_logging_doesnt_affect_other_loggers(caplog):
@ -28,3 +28,29 @@ def test_logging_doesnt_affect_other_loggers(caplog):
finally:
logging.getLogger("fastmcp").setLevel(original_level)
def test_configure_logging_with_traceback_kwargs():
"""Test that traceback-related kwargs can be passed without causing duplicate argument errors."""
# This should not raise TypeError about duplicate keyword arguments
configure_logging(enable_rich_tracebacks=True, tracebacks_max_frames=20)
# Verify the logger was configured
logger = logging.getLogger("fastmcp")
assert logger.handlers
assert len(logger.handlers) == 2 # One for normal logs, one for tracebacks
def test_configure_logging_traceback_defaults_can_be_overridden():
"""Test that default traceback settings can be overridden by kwargs."""
configure_logging(
enable_rich_tracebacks=True,
tracebacks_max_frames=20,
show_path=True,
show_level=True,
)
logger = logging.getLogger("fastmcp")
assert logger.handlers
# The traceback handler should have been created with custom values
# We can't directly inspect RichHandler internals easily, but we verified no error was raised