# 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. """ Logging configuration for Doris MCP Server. """ import logging import logging.config import sys from pathlib import Path from typing import Any def setup_logging( level: str = "INFO", log_file: str | None = None, log_format: str | None = None, ) -> None: """ Setup logging configuration. 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) def get_logger(name: str) -> logging.Logger: """ Get a logger instance. Args: name: Logger name Returns: Logger instance """ return logging.getLogger(name)