[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:
Mark McLoughlin
2026-10-01 09:11:46 +01:00
committed by GitHub
co-authored by OpenAI Codex
parent c4fc3c7d32
commit 91fcec1235
11 changed files with 148 additions and 46 deletions
+8 -4
View File
@@ -9,6 +9,7 @@ import vllm.envs as envs
from vllm.config.utils import config
LogLevel = Literal["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"]
LogFormatter = Literal["text", "json"]
@config
@@ -17,8 +18,8 @@ class LoggingConfig:
Supply a JSON object with ``--logging-config`` or individual fields with
dotted arguments such as ``--logging-config.log_level DEBUG`` and
``--logging-config.pylogging_config_file logging.json``. ``--log-level``
and legacy ``--log-config-file`` override their matching fields. Set
``--logging-config.formatter json``. ``--log-level`` and legacy
``--log-config-file`` override their matching fields. Set
``configure_logging`` to false to skip applying a ``dictConfig``.
"""
@@ -27,6 +28,9 @@ class LoggingConfig:
)
"""Log level used when no custom logging configuration is provided."""
formatter: LogFormatter = "text"
"""Formatter used by the built-in logging configuration."""
configure_logging: bool = Field(default_factory=lambda: envs.VLLM_CONFIGURE_LOGGING)
"""Whether to apply a Python logging configuration.
@@ -41,6 +45,6 @@ class LoggingConfig:
"""Path to a Python logging JSON file using
[``dictConfig`` schema](https://docs.python.org/3/library/logging.config.html#configuration-file-format).
A custom ``dictConfig`` is authoritative over ``log_level`` for logger,
handler, and formatter settings.
A custom ``dictConfig`` is authoritative over ``log_level`` and
``formatter`` for logger, handler, and formatter settings.
"""
@@ -8,6 +8,7 @@ from contextlib import asynccontextmanager
from fastapi import FastAPI
from vllm import envs
from vllm.config.logging import LogFormatter
from vllm.engine.protocol import EngineClient
from vllm.logger import init_logger
from vllm.utils.gc_utils import freeze_gc_heap
@@ -33,8 +34,8 @@ def get_uvicorn_log_config(args: Namespace) -> dict | None:
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
2. If disable_access_log_for_endpoints or JSON formatting is specified,
create a config with the requested filter and format
3. Otherwise, return None (use uvicorn defaults)
"""
# First, try to load from file if specified
@@ -48,19 +49,21 @@ def get_uvicorn_log_config(args: Namespace) -> dict | None:
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:
formatter: LogFormatter = (
logging_config.formatter if logging_config is not None else "text"
)
if args.disable_access_log_for_endpoints or formatter == "json":
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(",")
for p in (args.disable_access_log_for_endpoints or "").split(",")
if p.strip()
]
return create_uvicorn_log_config(
excluded_paths=excluded_paths,
log_level=args.uvicorn_log_level,
formatter=formatter,
)
return None
+16 -4
View File
@@ -20,6 +20,7 @@ from typing import TYPE_CHECKING, Any, Literal, cast
import vllm.envs as envs
from vllm.logging_utils import ColoredFormatter, NewLineFormatter
from vllm.logging_utils.formatter import JSON_FORMAT as _JSON_FORMAT
if TYPE_CHECKING:
from vllm.config.logging import LoggingConfig
@@ -30,7 +31,6 @@ _FORMAT = (
"[%(fileinfo)s:%(lineno)d] %(message)s"
)
_DATE_FORMAT = "%m-%d %H:%M:%S"
_vllm_process_info: tuple[str, int] | None = None
@@ -211,10 +211,12 @@ def _configure_vllm_root_logger(config: "LoggingConfig | None" = None) -> None:
configure_logging = envs.VLLM_CONFIGURE_LOGGING
log_level = envs.VLLM_LOGGING_LEVEL
log_config_file = envs.VLLM_LOGGING_CONFIG_PATH
log_formatter = "text"
else:
configure_logging = config.configure_logging
log_level = config.log_level
log_config_file = config.pylogging_config_file
log_formatter = config.formatter
if not configure_logging and log_config_file:
raise RuntimeError(
@@ -233,12 +235,22 @@ def _configure_vllm_root_logger(config: "LoggingConfig | None" = None) -> None:
# Refresh these values in case env vars have changed.
vllm_handler["level"] = log_level
vllm_handler["stream"] = envs.VLLM_LOGGING_STREAM
vllm_handler["formatter"] = "vllm_color" if _use_color() else "vllm"
for formatter in logging_config["formatters"].values():
formatter["log_level"] = log_level
if log_formatter == "json":
logging_config["formatters"] = {
"vllm_json": {
"class": "pythonjsonlogger.jsonlogger.JsonFormatter",
"format": _JSON_FORMAT,
}
}
vllm_handler["formatter"] = "vllm_json"
else:
vllm_handler["formatter"] = "vllm_color" if _use_color() else "vllm"
vllm_loggers = logging_config["loggers"]["vllm"]
vllm_loggers["level"] = log_level
for formatter in logging_config["formatters"].values():
formatter["log_level"] = log_level
if log_config_file:
if not path.exists(log_config_file):
+4 -4
View File
@@ -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
View File
@@ -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