feat: update log format

This commit is contained in:
Sun-ZhenXing
2025-09-22 17:56:39 +08:00
parent a42e3a2954
commit 8567b0804a
5 changed files with 91 additions and 41 deletions

View File

@@ -2,6 +2,8 @@ import sys
import click
from mcp_template_python.utils.log import UVICORN_LOGGING_CONFIG
from .__about__ import __module_name__, __version__
from .app import MCP_MAP
from .config import settings
@@ -25,6 +27,7 @@ def run_server(
host=host,
port=port,
reload=reload,
log_config=UVICORN_LOGGING_CONFIG,
**kwargs,
)

View File

@@ -0,0 +1,44 @@
from typing import Any
from rich.console import Console
from rich.logging import RichHandler
class UvicornRichHandler(RichHandler):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(
console=Console(stderr=True), rich_tracebacks=True, *args, **kwargs
)
UVICORN_LOGGING_CONFIG = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"default": {
"()": "uvicorn.logging.DefaultFormatter",
"fmt": "%(message)s",
"use_colors": False,
},
"access": {
"()": "uvicorn.logging.AccessFormatter",
"fmt": '%(client_addr)s - "%(request_line)s" %(status_code)s', # noqa: E501
"use_colors": False,
},
},
"handlers": {
"default": {
"formatter": "default",
"class": "mcp_template_python.utils.log.UvicornRichHandler",
},
"access": {
"formatter": "access",
"class": "mcp_template_python.utils.log.UvicornRichHandler",
},
},
"loggers": {
"uvicorn": {"handlers": ["default"], "level": "INFO", "propagate": False},
"uvicorn.error": {"level": "INFO"},
"uvicorn.access": {"handlers": ["access"], "level": "INFO", "propagate": False},
},
}