[Core][Logging] Add built-in JSON formatter (#58739)
Add `--logging-config.formatter=json` to enable the python-json-logger formatter without having to use a complete Python logging dictConfig. Signed-off-by: Mark McLoughlin <markmc@redhat.com> Co-authored-by: OpenAI Codex <noreply@openai.com>
This commit is contained in:
co-authored by
OpenAI Codex
parent
c4fc3c7d32
commit
91fcec1235
@@ -1,14 +1,14 @@
|
||||
# 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
|
||||
from vllm.logging_utils.torch_tensor import tensors_str_no_data
|
||||
from vllm.logging_utils.uvicorn_logging import (
|
||||
UvicornAccessLogFilter,
|
||||
create_uvicorn_log_config,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"NewLineFormatter",
|
||||
|
||||
@@ -4,6 +4,10 @@
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
JSON_FORMAT = (
|
||||
"%(asctime)s %(levelname)s %(name)s %(processName)s %(process)d %(message)s"
|
||||
)
|
||||
|
||||
|
||||
class NewLineFormatter(logging.Formatter):
|
||||
"""Adds logging prefix to newlines to align multi-line messages."""
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
# 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.
|
||||
"""
|
||||
"""Uvicorn logging configuration and access log filtering."""
|
||||
|
||||
import logging
|
||||
from typing import Literal
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from vllm.logging_utils.formatter import JSON_FORMAT
|
||||
|
||||
|
||||
class UvicornAccessLogFilter(logging.Filter):
|
||||
"""A logging filter that excludes access logs for specified endpoint paths.
|
||||
@@ -70,8 +68,9 @@ class UvicornAccessLogFilter(logging.Filter):
|
||||
def create_uvicorn_log_config(
|
||||
excluded_paths: list[str] | None = None,
|
||||
log_level: str = "info",
|
||||
formatter: Literal["text", "json"] = "text",
|
||||
) -> dict:
|
||||
"""Create a uvicorn logging configuration with access log filtering.
|
||||
"""Create a uvicorn logging configuration with optional access filtering.
|
||||
|
||||
This function generates a logging configuration dictionary that can be
|
||||
passed to uvicorn's `log_config` parameter. It sets up the access log
|
||||
@@ -80,6 +79,7 @@ def create_uvicorn_log_config(
|
||||
Args:
|
||||
excluded_paths: List of URL paths to exclude from access logs.
|
||||
log_level: The log level for uvicorn loggers.
|
||||
formatter: Format used for uvicorn logs.
|
||||
|
||||
Returns:
|
||||
A dictionary containing the logging configuration.
|
||||
@@ -140,4 +140,13 @@ def create_uvicorn_log_config(
|
||||
},
|
||||
},
|
||||
}
|
||||
if formatter == "json":
|
||||
json_formatter = {
|
||||
"class": "pythonjsonlogger.jsonlogger.JsonFormatter",
|
||||
"format": JSON_FORMAT,
|
||||
}
|
||||
config["formatters"] = {
|
||||
"default": json_formatter.copy(),
|
||||
"access": json_formatter.copy(),
|
||||
}
|
||||
return config
|
||||
Reference in New Issue
Block a user