From e8efcc335d9d062072d6bd90ed9eddf65a638d12 Mon Sep 17 00:00:00 2001 From: yangdx Date: Fri, 21 Feb 2025 17:53:01 +0800 Subject: [PATCH] Add access log filtering to reduce noise from high-frequency API endpoints --- lightrag/api/lightrag_server.py | 68 +++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/lightrag/api/lightrag_server.py b/lightrag/api/lightrag_server.py index f6cee412..b656b67f 100644 --- a/lightrag/api/lightrag_server.py +++ b/lightrag/api/lightrag_server.py @@ -19,20 +19,17 @@ from ascii_colors import ASCIIColors from fastapi.middleware.cors import CORSMiddleware from contextlib import asynccontextmanager from dotenv import load_dotenv - from .utils_api import ( get_api_key_dependency, parse_args, get_default_host, display_splash_screen, ) - from lightrag import LightRAG from lightrag.types import GPTKeywordExtractionFormat from lightrag.api import __api_version__ from lightrag.utils import EmbeddingFunc from lightrag.utils import logger - from .routers.document_routes import ( DocumentManager, create_document_routes, @@ -68,6 +65,38 @@ scan_progress: Dict = { progress_lock = threading.Lock() +class AccessLogFilter(logging.Filter): + def __init__(self): + super().__init__() + # Define paths to be filtered + self.filtered_paths = ["/documents", "/health", "/webui/"] + + def filter(self, record): + try: + if not hasattr(record, "args") or not isinstance(record.args, tuple): + return True + if len(record.args) < 5: + return True + + method = record.args[1] + path = record.args[2] + status = record.args[4] + # print(f"Debug - Method: {method}, Path: {path}, Status: {status}") + # print(f"Debug - Filtered paths: {self.filtered_paths}") + + if ( + method == "GET" + and (status == 200 or status == 304) + and path in self.filtered_paths + ): + return False + + return True + + except Exception: + return True + + def create_app(args): # Set global top_k global global_top_k @@ -409,6 +438,38 @@ def create_app(args): def main(): args = parse_args() import uvicorn + import logging.config + + # Configure uvicorn logging + logging.config.dictConfig( + { + "version": 1, + "disable_existing_loggers": False, + "formatters": { + "default": { + "format": "%(levelname)s: %(message)s", + }, + }, + "handlers": { + "default": { + "formatter": "default", + "class": "logging.StreamHandler", + "stream": "ext://sys.stderr", + }, + }, + "loggers": { + "uvicorn.access": { + "handlers": ["default"], + "level": "INFO", + "propagate": False, + }, + }, + } + ) + + # Add filter to uvicorn access logger + uvicorn_access_logger = logging.getLogger("uvicorn.access") + uvicorn_access_logger.addFilter(AccessLogFilter()) app = create_app(args) display_splash_screen(args) @@ -416,6 +477,7 @@ def main(): "app": app, "host": args.host, "port": args.port, + "log_config": None, # Disable default config } if args.ssl: uvicorn_config.update(