mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-22 05:24:18 +02:00
Merge pull request #155 from jlowin/local-logs
Only apply log config to FastMCP loggers
This commit is contained in:
commit
a30ee7062c
3 changed files with 44 additions and 6 deletions
|
|
@ -20,15 +20,23 @@ def get_logger(name: str) -> logging.Logger:
|
|||
|
||||
|
||||
def configure_logging(
|
||||
level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "INFO",
|
||||
level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] | int = "INFO",
|
||||
) -> None:
|
||||
"""Configure logging for FastMCP.
|
||||
|
||||
Args:
|
||||
level: the log level to use
|
||||
"""
|
||||
logging.basicConfig(
|
||||
level=level,
|
||||
format="%(message)s",
|
||||
handlers=[RichHandler(console=Console(stderr=True), rich_tracebacks=True)],
|
||||
)
|
||||
# Only configure the FastMCP logger namespace
|
||||
handler = RichHandler(console=Console(stderr=True), rich_tracebacks=True)
|
||||
formatter = logging.Formatter("%(message)s")
|
||||
handler.setFormatter(formatter)
|
||||
|
||||
fastmcp_logger = logging.getLogger("FastMCP")
|
||||
fastmcp_logger.setLevel(level)
|
||||
|
||||
# Remove any existing handlers to avoid duplicates on reconfiguration
|
||||
for hdlr in fastmcp_logger.handlers[:]:
|
||||
fastmcp_logger.removeHandler(hdlr)
|
||||
|
||||
fastmcp_logger.addHandler(handler)
|
||||
|
|
|
|||
0
tests/conftest.py
Normal file
0
tests/conftest.py
Normal file
30
tests/utilities/test_logging.py
Normal file
30
tests/utilities/test_logging.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import logging
|
||||
|
||||
from fastmcp.utilities.logging import get_logger
|
||||
|
||||
|
||||
def test_logging_doesnt_affect_other_loggers(caplog):
|
||||
# set FastMCP loggers to CRITICAL and ensure other loggers still emit messages
|
||||
original_level = logging.getLogger("FastMCP").getEffectiveLevel()
|
||||
|
||||
try:
|
||||
logging.getLogger("FastMCP").setLevel(logging.CRITICAL)
|
||||
|
||||
root_logger = logging.getLogger()
|
||||
app_logger = logging.getLogger("app")
|
||||
fastmcp_logger = logging.getLogger("FastMCP")
|
||||
fastmcp_server_logger = get_logger("server")
|
||||
|
||||
with caplog.at_level(logging.INFO):
|
||||
root_logger.info("--ROOT--")
|
||||
app_logger.info("--APP--")
|
||||
fastmcp_logger.info("--FASTMCP--")
|
||||
fastmcp_server_logger.info("--FASTMCP SERVER--")
|
||||
|
||||
assert "--ROOT--" in caplog.text
|
||||
assert "--APP--" in caplog.text
|
||||
assert "--FASTMCP--" not in caplog.text
|
||||
assert "--FASTMCP SERVER--" not in caplog.text
|
||||
|
||||
finally:
|
||||
logging.getLogger("FastMCP").setLevel(original_level)
|
||||
Loading…
Add table
Add a link
Reference in a new issue