[Logging] add --disable-access-log-for-endpoints CLI option (#30011)
Add a new CLI option --disable-access-log-for-endpoints to suppress uvicorn access logs for specified endpoints (e.g., /health, /metrics, /ping). This addresses the need to reduce log noise in production environments where health check endpoints are frequently polled by load balancers or monitoring systems, generating excessive log entries that obscure meaningful request logs. Fixes #29982 Signed-off-by: JaredforReal <w13431838023@gmail.com>
This commit is contained in:
@@ -264,6 +264,39 @@ def load_log_config(log_config_file: str | None) -> dict | None:
|
||||
return None
|
||||
|
||||
|
||||
def get_uvicorn_log_config(args: Namespace) -> dict | None:
|
||||
"""
|
||||
Get the uvicorn log config based on the provided arguments.
|
||||
|
||||
Priority:
|
||||
1. If log_config_file is specified, use it
|
||||
2. If disable_access_log_for_endpoints is specified, create a config with
|
||||
the access log filter
|
||||
3. Otherwise, return None (use uvicorn defaults)
|
||||
"""
|
||||
# First, try to load from file if specified
|
||||
log_config = load_log_config(args.log_config_file)
|
||||
if log_config is not None:
|
||||
return log_config
|
||||
|
||||
# If endpoints to filter are specified, create a config with the filter
|
||||
if args.disable_access_log_for_endpoints:
|
||||
from vllm.logging_utils import create_uvicorn_log_config
|
||||
|
||||
# Parse comma-separated string into list
|
||||
excluded_paths = [
|
||||
p.strip()
|
||||
for p in args.disable_access_log_for_endpoints.split(",")
|
||||
if p.strip()
|
||||
]
|
||||
return create_uvicorn_log_config(
|
||||
excluded_paths=excluded_paths,
|
||||
log_level=args.uvicorn_log_level,
|
||||
)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
class AuthenticationMiddleware:
|
||||
"""
|
||||
Pure ASGI middleware that authenticates each request by checking
|
||||
@@ -930,8 +963,8 @@ async def run_server_worker(
|
||||
if args.reasoning_parser_plugin and len(args.reasoning_parser_plugin) > 3:
|
||||
ReasoningParserManager.import_reasoning_parser(args.reasoning_parser_plugin)
|
||||
|
||||
# Load logging config for uvicorn if specified
|
||||
log_config = load_log_config(args.log_config_file)
|
||||
# Get uvicorn log config (from file or with endpoint filter)
|
||||
log_config = get_uvicorn_log_config(args)
|
||||
if log_config is not None:
|
||||
uvicorn_kwargs["log_config"] = log_config
|
||||
|
||||
|
||||
@@ -85,6 +85,12 @@ class FrontendArgs:
|
||||
"""Log level for uvicorn."""
|
||||
disable_uvicorn_access_log: bool = False
|
||||
"""Disable uvicorn access log."""
|
||||
disable_access_log_for_endpoints: str | None = None
|
||||
"""Comma-separated list of endpoint paths to exclude from uvicorn access
|
||||
logs. This is useful to reduce log noise from high-frequency endpoints
|
||||
like health checks. Example: "/health,/metrics,/ping".
|
||||
When set, access logs for requests to these paths will be suppressed
|
||||
while keeping logs for other endpoints."""
|
||||
allow_credentials: bool = False
|
||||
"""Allow credentials."""
|
||||
allowed_origins: list[str] = field(default_factory=lambda: ["*"])
|
||||
@@ -244,6 +250,11 @@ class FrontendArgs:
|
||||
del frontend_kwargs["middleware"]["nargs"]
|
||||
frontend_kwargs["middleware"]["default"] = []
|
||||
|
||||
# Special case: disable_access_log_for_endpoints is a single
|
||||
# comma-separated string, not a list
|
||||
if "nargs" in frontend_kwargs["disable_access_log_for_endpoints"]:
|
||||
del frontend_kwargs["disable_access_log_for_endpoints"]["nargs"]
|
||||
|
||||
# Special case: Tool call parser shows built-in options.
|
||||
valid_tool_parsers = list(ToolParserManager.list_registered())
|
||||
parsers_str = ",".join(valid_tool_parsers)
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from vllm.logging_utils.access_log_filter import (
|
||||
UvicornAccessLogFilter,
|
||||
create_uvicorn_log_config,
|
||||
)
|
||||
from vllm.logging_utils.formatter import ColoredFormatter, NewLineFormatter
|
||||
from vllm.logging_utils.lazy import lazy
|
||||
from vllm.logging_utils.log_time import logtime
|
||||
@@ -8,6 +12,8 @@ from vllm.logging_utils.log_time import logtime
|
||||
__all__ = [
|
||||
"NewLineFormatter",
|
||||
"ColoredFormatter",
|
||||
"UvicornAccessLogFilter",
|
||||
"create_uvicorn_log_config",
|
||||
"lazy",
|
||||
"logtime",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""
|
||||
Access log filter for uvicorn to exclude specific endpoints from logging.
|
||||
|
||||
This module provides a logging filter that can be used to suppress access logs
|
||||
for specific endpoints (e.g., /health, /metrics) to reduce log noise in
|
||||
production environments.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from urllib.parse import urlparse
|
||||
|
||||
|
||||
class UvicornAccessLogFilter(logging.Filter):
|
||||
"""
|
||||
A logging filter that excludes access logs for specified endpoint paths.
|
||||
|
||||
This filter is designed to work with uvicorn's access logger. It checks
|
||||
the log record's arguments for the request path and filters out records
|
||||
matching the excluded paths.
|
||||
|
||||
Uvicorn access log format:
|
||||
'%s - "%s %s HTTP/%s" %d'
|
||||
(client_addr, method, path, http_version, status_code)
|
||||
|
||||
Example:
|
||||
127.0.0.1:12345 - "GET /health HTTP/1.1" 200
|
||||
|
||||
Args:
|
||||
excluded_paths: A list of URL paths to exclude from logging.
|
||||
Paths are matched exactly.
|
||||
Example: ["/health", "/metrics"]
|
||||
"""
|
||||
|
||||
def __init__(self, excluded_paths: list[str] | None = None):
|
||||
super().__init__()
|
||||
self.excluded_paths = set(excluded_paths or [])
|
||||
|
||||
def filter(self, record: logging.LogRecord) -> bool:
|
||||
"""
|
||||
Determine if the log record should be logged.
|
||||
|
||||
Args:
|
||||
record: The log record to evaluate.
|
||||
|
||||
Returns:
|
||||
True if the record should be logged, False otherwise.
|
||||
"""
|
||||
if not self.excluded_paths:
|
||||
return True
|
||||
|
||||
# This filter is specific to uvicorn's access logs.
|
||||
if record.name != "uvicorn.access":
|
||||
return True
|
||||
|
||||
# The path is the 3rd argument in the log record's args tuple.
|
||||
# See uvicorn's access logging implementation for details.
|
||||
log_args = record.args
|
||||
if isinstance(log_args, tuple) and len(log_args) >= 3:
|
||||
path_with_query = log_args[2]
|
||||
# Get path component without query string.
|
||||
if isinstance(path_with_query, str):
|
||||
path = urlparse(path_with_query).path
|
||||
if path in self.excluded_paths:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def create_uvicorn_log_config(
|
||||
excluded_paths: list[str] | None = None,
|
||||
log_level: str = "info",
|
||||
) -> dict:
|
||||
"""
|
||||
Create a uvicorn logging configuration with access log filtering.
|
||||
|
||||
This function generates a logging configuration dictionary that can be
|
||||
passed to uvicorn's `log_config` parameter. It sets up the access log
|
||||
filter to exclude specified paths.
|
||||
|
||||
Args:
|
||||
excluded_paths: List of URL paths to exclude from access logs.
|
||||
log_level: The log level for uvicorn loggers.
|
||||
|
||||
Returns:
|
||||
A dictionary containing the logging configuration.
|
||||
|
||||
Example:
|
||||
>>> config = create_uvicorn_log_config(["/health", "/metrics"])
|
||||
>>> uvicorn.run(app, log_config=config)
|
||||
"""
|
||||
config = {
|
||||
"version": 1,
|
||||
"disable_existing_loggers": False,
|
||||
"filters": {
|
||||
"access_log_filter": {
|
||||
"()": UvicornAccessLogFilter,
|
||||
"excluded_paths": excluded_paths or [],
|
||||
},
|
||||
},
|
||||
"formatters": {
|
||||
"default": {
|
||||
"()": "uvicorn.logging.DefaultFormatter",
|
||||
"fmt": "%(levelprefix)s %(message)s",
|
||||
"use_colors": None,
|
||||
},
|
||||
"access": {
|
||||
"()": "uvicorn.logging.AccessFormatter",
|
||||
"fmt": '%(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s', # noqa: E501
|
||||
},
|
||||
},
|
||||
"handlers": {
|
||||
"default": {
|
||||
"formatter": "default",
|
||||
"class": "logging.StreamHandler",
|
||||
"stream": "ext://sys.stderr",
|
||||
},
|
||||
"access": {
|
||||
"formatter": "access",
|
||||
"class": "logging.StreamHandler",
|
||||
"stream": "ext://sys.stdout",
|
||||
"filters": ["access_log_filter"],
|
||||
},
|
||||
},
|
||||
"loggers": {
|
||||
"uvicorn": {
|
||||
"handlers": ["default"],
|
||||
"level": log_level.upper(),
|
||||
"propagate": False,
|
||||
},
|
||||
"uvicorn.error": {
|
||||
"level": log_level.upper(),
|
||||
"handlers": ["default"],
|
||||
"propagate": False,
|
||||
},
|
||||
"uvicorn.access": {
|
||||
"handlers": ["access"],
|
||||
"level": log_level.upper(),
|
||||
"propagate": False,
|
||||
},
|
||||
},
|
||||
}
|
||||
return config
|
||||
Reference in New Issue
Block a user