add rich handler and dotenv loading for settings

This commit is contained in:
zzstoatzz 2024-11-30 19:02:01 -06:00
commit c700dcd1e3
3 changed files with 21 additions and 16 deletions

View file

@ -13,7 +13,7 @@ from typing_extensions import Annotated
from ..utilities.logging import get_logger
from . import claude
logger = get_logger(__name__)
logger = get_logger("cli")
app = typer.Typer(
name="fastmcp",
@ -352,7 +352,7 @@ def install(
with_packages=with_packages,
force=force,
):
print(f"Successfully installed {name} in Claude app")
logger.info(f"Successfully installed {name} in Claude app")
else:
print(f"Failed to install {name} in Claude app")
logger.error(f"Failed to install {name} in Claude app")
sys.exit(1)

View file

@ -53,7 +53,11 @@ class Settings(BaseSettings):
For example, FASTMCP_DEBUG=true will set debug=True.
"""
model_config: SettingsConfigDict = SettingsConfigDict(env_prefix="FASTMCP_")
model_config: SettingsConfigDict = SettingsConfigDict(
env_prefix="FASTMCP_",
env_file=".env",
extra="ignore",
)
# Server settings
debug: bool = False

View file

@ -1,30 +1,31 @@
"""Logging utilities for FastMCP."""
"""logging utilities for fastmcp"""
import logging
from typing import Literal
from rich.logging import RichHandler
def get_logger(name: str) -> logging.Logger:
"""Get a logger nested under FastMCP namespace.
"""get a logger nested under fastmcp namespace.
Args:
name: The name of the logger, which will be prefixed with 'FastMCP.'
args:
name: the name of the logger, which will be prefixed with 'fastmcp.'
Returns:
A configured logger instance
returns:
a configured logger instance
"""
return logging.getLogger(f"FastMCP.{name}")
return logging.getLogger(f"fastmcp.{name}")
def configure_logging(
level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "INFO",
) -> None:
"""Configure logging for FastMCP.
"""configure logging for fastmcp.
Args:
level: The log level to use
args:
level: the log level to use
"""
logging.basicConfig(
level=level,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=level, format="%(message)s", handlers=[RichHandler(rich_tracebacks=True)]
)