Files
doris-mcp-server/doris_mcp_server/utils/logger.py

102 lines
2.9 KiB
Python
Raw Normal View History

2025-06-08 19:22:13 +08:00
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
2025-05-06 12:56:55 +08:00
"""
2025-06-08 18:44:40 +08:00
Logging configuration for Doris MCP Server.
2025-05-06 12:56:55 +08:00
"""
import logging
2025-06-08 18:44:40 +08:00
import logging.config
import sys
2025-05-06 12:56:55 +08:00
from pathlib import Path
2025-06-08 18:44:40 +08:00
from typing import Any
2025-05-06 12:56:55 +08:00
2025-06-08 18:44:40 +08:00
def setup_logging(
level: str = "INFO",
log_file: str | None = None,
log_format: str | None = None,
) -> None:
"""
Setup logging configuration.
2025-05-06 12:56:55 +08:00
2025-06-08 18:44:40 +08:00
Args:
level: Logging level (DEBUG, INFO, WARNING, ERROR)
log_file: Optional log file path
log_format: Optional custom log format
"""
if log_format is None:
log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
# Base configuration
config: dict[str, Any] = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"default": {"format": log_format, "datefmt": "%Y-%m-%d %H:%M:%S"}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": level,
"formatter": "default",
"stream": sys.stdout,
}
},
"root": {"level": level, "handlers": ["console"]},
"loggers": {
"doris_mcp_server": {
"level": level,
"handlers": ["console"],
"propagate": False,
}
},
}
# Add file handler if log_file is specified
if log_file:
# Ensure log directory exists
log_path = Path(log_file)
log_path.parent.mkdir(parents=True, exist_ok=True)
config["handlers"]["file"] = {
"class": "logging.handlers.RotatingFileHandler",
"level": level,
"formatter": "default",
"filename": log_file,
"maxBytes": 10485760, # 10MB
"backupCount": 5,
}
# Add file handler to root and package loggers
config["root"]["handlers"].append("file")
config["loggers"]["doris_mcp_server"]["handlers"].append("file")
logging.config.dictConfig(config)
2025-05-06 12:56:55 +08:00
def get_logger(name: str) -> logging.Logger:
"""
2025-06-08 18:44:40 +08:00
Get a logger instance.
2025-05-06 12:56:55 +08:00
Args:
name: Logger name
2025-06-08 18:44:40 +08:00
2025-05-06 12:56:55 +08:00
Returns:
2025-06-08 18:44:40 +08:00
Logger instance
2025-05-06 12:56:55 +08:00
"""
2025-06-08 18:44:40 +08:00
return logging.getLogger(name)