File size: 1,508 Bytes
c1dbfd7
7ca2f08
4461926
6d09a2a
7ca2f08
e947e9b
e1f78df
 
4461926
7ca2f08
13bdbd3
4461926
c1dbfd7
13bdbd3
7ca2f08
c1dbfd7
e1f78df
4461926
13bdbd3
7ca2f08
4461926
7ca2f08
bd185a2
ea8c89c
acef890
6d09a2a
7ca2f08
ea8c89c
 
4461926
c1dbfd7
ea8c89c
e1f78df
6d09a2a
4461926
acef890
ea8c89c
 
 
bd185a2
acef890
 
 
6d09a2a
acef890
bd185a2
 
 
ea8c89c
bd185a2
 
ea8c89c
 
bd185a2
ea8c89c
6acea03
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Logging utilities for FastMCP."""

import logging
from typing import Any, Literal

from rich.console import Console
from rich.logging import RichHandler


def get_logger(name: str) -> logging.Logger:
    """Get a logger nested under FastMCP namespace.

    Args:
        name: the name of the logger, which will be prefixed with 'FastMCP.'

    Returns:
        a configured logger instance
    """
    return logging.getLogger(f"FastMCP.{name}")


def configure_logging(
    level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] | int = "INFO",
    logger: logging.Logger | None = None,
    enable_rich_tracebacks: bool = True,
    **rich_kwargs: Any,
) -> None:
    """
    Configure logging for FastMCP.

    Args:
        logger: the logger to configure
        level: the log level to use
        rich_kwargs: the parameters to use for creating RichHandler
    """

    if logger is None:
        logger = logging.getLogger("FastMCP")

    # Only configure the FastMCP logger namespace
    handler = RichHandler(
        console=Console(stderr=True),
        rich_tracebacks=enable_rich_tracebacks,
        **rich_kwargs,
    )
    formatter = logging.Formatter("%(message)s")
    handler.setFormatter(formatter)

    logger.setLevel(level)

    # Remove any existing handlers to avoid duplicates on reconfiguration
    for hdlr in logger.handlers[:]:
        logger.removeHandler(hdlr)

    logger.addHandler(handler)

    # Don't propagate to the root logger
    logger.propagate = False