Add access log filtering to reduce noise from high-frequency API endpoints
This commit is contained in:
@@ -19,20 +19,17 @@ from ascii_colors import ASCIIColors
|
|||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
from .utils_api import (
|
from .utils_api import (
|
||||||
get_api_key_dependency,
|
get_api_key_dependency,
|
||||||
parse_args,
|
parse_args,
|
||||||
get_default_host,
|
get_default_host,
|
||||||
display_splash_screen,
|
display_splash_screen,
|
||||||
)
|
)
|
||||||
|
|
||||||
from lightrag import LightRAG
|
from lightrag import LightRAG
|
||||||
from lightrag.types import GPTKeywordExtractionFormat
|
from lightrag.types import GPTKeywordExtractionFormat
|
||||||
from lightrag.api import __api_version__
|
from lightrag.api import __api_version__
|
||||||
from lightrag.utils import EmbeddingFunc
|
from lightrag.utils import EmbeddingFunc
|
||||||
from lightrag.utils import logger
|
from lightrag.utils import logger
|
||||||
|
|
||||||
from .routers.document_routes import (
|
from .routers.document_routes import (
|
||||||
DocumentManager,
|
DocumentManager,
|
||||||
create_document_routes,
|
create_document_routes,
|
||||||
@@ -68,6 +65,38 @@ scan_progress: Dict = {
|
|||||||
progress_lock = threading.Lock()
|
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):
|
def create_app(args):
|
||||||
# Set global top_k
|
# Set global top_k
|
||||||
global global_top_k
|
global global_top_k
|
||||||
@@ -409,6 +438,38 @@ def create_app(args):
|
|||||||
def main():
|
def main():
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
import uvicorn
|
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)
|
app = create_app(args)
|
||||||
display_splash_screen(args)
|
display_splash_screen(args)
|
||||||
@@ -416,6 +477,7 @@ def main():
|
|||||||
"app": app,
|
"app": app,
|
||||||
"host": args.host,
|
"host": args.host,
|
||||||
"port": args.port,
|
"port": args.port,
|
||||||
|
"log_config": None, # Disable default config
|
||||||
}
|
}
|
||||||
if args.ssl:
|
if args.ssl:
|
||||||
uvicorn_config.update(
|
uvicorn_config.update(
|
||||||
|
Reference in New Issue
Block a user