main
py 152 lines 4.85 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """Uvicorn logging configuration and access log filtering."""
4
5 import logging
6 from typing import Literal
7 from urllib.parse import urlparse
8
9 from vllm.logging_utils.formatter import JSON_FORMAT
10
11
12 class UvicornAccessLogFilter(logging.Filter):
13 """A logging filter that excludes access logs for specified endpoint paths.
14
15 This filter is designed to work with uvicorn's access logger. It checks
16 the log record's arguments for the request path and filters out records
17 matching the excluded paths.
18
19 Uvicorn access log format:
20 '%s - "%s %s HTTP/%s" %d'
21 (client_addr, method, path, http_version, status_code)
22
23 Example:
24 127.0.0.1:12345 - "GET /health HTTP/1.1" 200
25
26 Args:
27 excluded_paths: A list of URL paths to exclude from logging.
28 Paths are matched exactly.
29 Example: ["/health", "/metrics"]
30
31 """
32
33 def __init__(self, excluded_paths: list[str] | None = None):
34 super().__init__()
35 self.excluded_paths = set(excluded_paths or [])
36
37 def filter(self, record: logging.LogRecord) -> bool:
38 """Determine if the log record should be logged.
39
40 Args:
41 record: The log record to evaluate.
42
43 Returns:
44 True if the record should be logged, False otherwise.
45
46 """
47 if not self.excluded_paths:
48 return True
49
50 # This filter is specific to uvicorn's access logs.
51 if record.name != "uvicorn.access":
52 return True
53
54 # The path is the 3rd argument in the log record's args tuple.
55 # See uvicorn's access logging implementation for details.
56 log_args = record.args
57 if isinstance(log_args, tuple) and len(log_args) >= 3:
58 path_with_query = log_args[2]
59 # Get path component without query string.
60 if isinstance(path_with_query, str):
61 path = urlparse(path_with_query).path
62 if path in self.excluded_paths:
63 return False
64
65 return True
66
67
68 def create_uvicorn_log_config(
69 excluded_paths: list[str] | None = None,
70 log_level: str = "info",
71 formatter: Literal["text", "json"] = "text",
72 ) -> dict:
73 """Create a uvicorn logging configuration with optional access filtering.
74
75 This function generates a logging configuration dictionary that can be
76 passed to uvicorn's `log_config` parameter. It sets up the access log
77 filter to exclude specified paths.
78
79 Args:
80 excluded_paths: List of URL paths to exclude from access logs.
81 log_level: The log level for uvicorn loggers.
82 formatter: Format used for uvicorn logs.
83
84 Returns:
85 A dictionary containing the logging configuration.
86
87 Example:
88 >>> config = create_uvicorn_log_config(["/health", "/metrics"])
89 >>> uvicorn.run(app, log_config=config)
90
91 """
92 config = {
93 "version": 1,
94 "disable_existing_loggers": False,
95 "filters": {
96 "access_log_filter": {
97 "()": UvicornAccessLogFilter,
98 "excluded_paths": excluded_paths or [],
99 },
100 },
101 "formatters": {
102 "default": {
103 "()": "uvicorn.logging.DefaultFormatter",
104 "fmt": "%(levelprefix)s %(message)s",
105 "use_colors": None,
106 },
107 "access": {
108 "()": "uvicorn.logging.AccessFormatter",
109 "fmt": '%(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s', # noqa: E501
110 },
111 },
112 "handlers": {
113 "default": {
114 "formatter": "default",
115 "class": "logging.StreamHandler",
116 "stream": "ext://sys.stderr",
117 },
118 "access": {
119 "formatter": "access",
120 "class": "logging.StreamHandler",
121 "stream": "ext://sys.stdout",
122 "filters": ["access_log_filter"],
123 },
124 },
125 "loggers": {
126 "uvicorn": {
127 "handlers": ["default"],
128 "level": log_level.upper(),
129 "propagate": False,
130 },
131 "uvicorn.error": {
132 "level": log_level.upper(),
133 "handlers": ["default"],
134 "propagate": False,
135 },
136 "uvicorn.access": {
137 "handlers": ["access"],
138 "level": log_level.upper(),
139 "propagate": False,
140 },
141 },
142 }
143 if formatter == "json":
144 json_formatter = {
145 "class": "pythonjsonlogger.jsonlogger.JsonFormatter",
146 "format": JSON_FORMAT,
147 }
148 config["formatters"] = {
149 "default": json_formatter.copy(),
150 "access": json_formatter.copy(),
151 }
152 return config