Jeremiah Lowin commited on
Commit
bd185a2
·
1 Parent(s): 6c5c191

Only apply log config to FastMCP loggers

Browse files
src/fastmcp/utilities/logging.py CHANGED
@@ -20,15 +20,23 @@ def get_logger(name: str) -> logging.Logger:
20
 
21
 
22
  def configure_logging(
23
- level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "INFO",
24
  ) -> None:
25
  """Configure logging for FastMCP.
26
 
27
  Args:
28
  level: the log level to use
29
  """
30
- logging.basicConfig(
31
- level=level,
32
- format="%(message)s",
33
- handlers=[RichHandler(console=Console(stderr=True), rich_tracebacks=True)],
34
- )
 
 
 
 
 
 
 
 
 
20
 
21
 
22
  def configure_logging(
23
+ level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] | int = "INFO",
24
  ) -> None:
25
  """Configure logging for FastMCP.
26
 
27
  Args:
28
  level: the log level to use
29
  """
30
+ # Only configure the FastMCP logger namespace
31
+ handler = RichHandler(console=Console(stderr=True), rich_tracebacks=True)
32
+ formatter = logging.Formatter("%(message)s")
33
+ handler.setFormatter(formatter)
34
+
35
+ fastmcp_logger = logging.getLogger("FastMCP")
36
+ fastmcp_logger.setLevel(level)
37
+
38
+ # Remove any existing handlers to avoid duplicates on reconfiguration
39
+ for hdlr in fastmcp_logger.handlers[:]:
40
+ fastmcp_logger.removeHandler(hdlr)
41
+
42
+ fastmcp_logger.addHandler(handler)
tests/conftest.py ADDED
File without changes
tests/utilities/test_logging.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+
3
+ from fastmcp.utilities.logging import get_logger
4
+
5
+
6
+ def test_logging_doesnt_affect_other_loggers(caplog):
7
+ # set FastMCP loggers to CRITICAL and ensure other loggers still emit messages
8
+ original_level = logging.getLogger("FastMCP").getEffectiveLevel()
9
+
10
+ try:
11
+ logging.getLogger("FastMCP").setLevel(logging.CRITICAL)
12
+
13
+ root_logger = logging.getLogger()
14
+ app_logger = logging.getLogger("app")
15
+ fastmcp_logger = logging.getLogger("FastMCP")
16
+ fastmcp_server_logger = get_logger("server")
17
+
18
+ with caplog.at_level(logging.INFO):
19
+ root_logger.info("--ROOT--")
20
+ app_logger.info("--APP--")
21
+ fastmcp_logger.info("--FASTMCP--")
22
+ fastmcp_server_logger.info("--FASTMCP SERVER--")
23
+
24
+ assert "--ROOT--" in caplog.text
25
+ assert "--APP--" in caplog.text
26
+ assert "--FASTMCP--" not in caplog.text
27
+ assert "--FASTMCP SERVER--" not in caplog.text
28
+
29
+ finally:
30
+ logging.getLogger("FastMCP").setLevel(original_level)