Skip to content

Commit 1f325f2

Browse files
committed
add logging to examples (override uvicorn's default logger)
1 parent 7f543d9 commit 1f325f2

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

examples/full_schema_description_example.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
"""
44

55
from examples.apps import items
6+
from examples.setup import setup_logging
67

78
from fastapi_mcp import FastApiMCP
89

910

11+
setup_logging()
12+
13+
1014
# Add MCP server to the FastAPI app
1115
mcp = FastApiMCP(
1216
items.app,

examples/setup.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import logging
2+
3+
from pydantic import BaseModel
4+
5+
6+
class LoggingConfig(BaseModel):
7+
LOGGER_NAME: str = "fastapi_mcp"
8+
LOG_FORMAT: str = "%(levelprefix)s %(asctime)s\t[%(name)s] %(message)s"
9+
LOG_LEVEL: str = logging.getLevelName(logging.INFO)
10+
11+
version: int = 1
12+
disable_existing_loggers: bool = False
13+
formatters: dict = {
14+
"default": {
15+
"()": "uvicorn.logging.DefaultFormatter",
16+
"fmt": LOG_FORMAT,
17+
"datefmt": "%Y-%m-%d %H:%M:%S",
18+
},
19+
}
20+
handlers: dict = {
21+
"default": {
22+
"formatter": "default",
23+
"class": "logging.StreamHandler",
24+
"stream": "ext://sys.stdout",
25+
},
26+
}
27+
loggers: dict = {
28+
"": {"handlers": ["default"], "level": LOG_LEVEL}, # Root logger
29+
"uvicorn": {"handlers": ["default"], "level": LOG_LEVEL},
30+
LOGGER_NAME: {"handlers": ["default"], "level": LOG_LEVEL},
31+
}
32+
33+
34+
def setup_logging():
35+
from logging.config import dictConfig
36+
37+
logging_config = LoggingConfig()
38+
dictConfig(logging_config.model_dump())

examples/simple_example.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
"""
44

55
from examples.apps import items
6+
from examples.setup import setup_logging
67

78
from fastapi_mcp import FastApiMCP
89

910

11+
setup_logging()
12+
13+
1014
# Add MCP server to the FastAPI app
1115
mcp = FastApiMCP(
1216
items.app,

0 commit comments

Comments
 (0)