[Core][Logging] Fix JSON logging process decoration (#57957)
Signed-off-by: Mark McLoughlin <markmc@redhat.com>
This commit is contained in:
@@ -75,7 +75,8 @@ To begin, first, create an appropriate JSON logging configuration file:
|
||||
{
|
||||
"formatters": {
|
||||
"json": {
|
||||
"class": "pythonjsonlogger.jsonlogger.JsonFormatter"
|
||||
"class": "pythonjsonlogger.jsonlogger.JsonFormatter",
|
||||
"format": "%(asctime)s %(levelname)s %(name)s %(vllm_process_name)s %(process)d %(message)s"
|
||||
}
|
||||
},
|
||||
"handlers": {
|
||||
@@ -104,6 +105,22 @@ vllm serve mistralai/Mistral-7B-v0.1 --max-model-len 2048 \
|
||||
--logging-config.pylogging_config_file /path/to/logging_config.json
|
||||
```
|
||||
|
||||
Each vLLM log record is one JSON object and includes `vllm_process_name`, which
|
||||
identifies vLLM's logical process (including worker ranks where applicable).
|
||||
The standard `process` record attribute contains the operating-system PID.
|
||||
vLLM avoids altering `stdout` or `stderr`, so no text is prepended to JSON
|
||||
log records.
|
||||
|
||||
This applies to records emitted through the configured Python loggers. A JSON
|
||||
formatter cannot convert unrelated output into JSON, such as a third-party
|
||||
library writing directly to `stdout` or `stderr`; configure or route such
|
||||
output separately when a consumer requires every collected line to be JSON.
|
||||
|
||||
When serving, `VLLM_LOGGING_CONFIG_PATH` is also used as Uvicorn's logging
|
||||
configuration. This example configures only `vllm`; configure `uvicorn`,
|
||||
`uvicorn.error`, and `uvicorn.access` with a JSON handler when those server
|
||||
logs must also be structured.
|
||||
|
||||
### Example 2: Silence a particular vLLM logger
|
||||
|
||||
To silence a particular vLLM logger, it is necessary to provide custom logging
|
||||
|
||||
@@ -16,6 +16,7 @@ from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
|
||||
import vllm.logger as vllm_logger
|
||||
from vllm.config import LoggingConfig
|
||||
from vllm.logger import (
|
||||
_DATE_FORMAT,
|
||||
@@ -28,6 +29,7 @@ from vllm.logger import (
|
||||
)
|
||||
from vllm.logging_utils import NewLineFormatter
|
||||
from vllm.logging_utils.dump_input import prepare_object_to_dump
|
||||
from vllm.utils.system_utils import decorate_logs
|
||||
|
||||
|
||||
def f1(x):
|
||||
@@ -110,6 +112,59 @@ def test_offline_llm_configures_logging_before_logging_args(monkeypatch):
|
||||
llm_module.LLM(model="facebook/opt-125m")
|
||||
|
||||
|
||||
def test_json_logging(monkeypatch, tmp_path):
|
||||
output = io.StringIO()
|
||||
logging_config = {
|
||||
"version": 1,
|
||||
"disable_existing_loggers": False,
|
||||
"formatters": {
|
||||
"json": {
|
||||
"class": "pythonjsonlogger.jsonlogger.JsonFormatter",
|
||||
"format": (
|
||||
"%(asctime)s %(levelname)s %(name)s %(vllm_process_name)s "
|
||||
"%(process)d %(message)s"
|
||||
),
|
||||
},
|
||||
},
|
||||
"handlers": {
|
||||
"console": {
|
||||
"class": "logging.StreamHandler",
|
||||
"formatter": "json",
|
||||
"stream": "ext://sys.stdout",
|
||||
},
|
||||
},
|
||||
"loggers": {
|
||||
"vllm": {
|
||||
"handlers": ["console"],
|
||||
"level": "INFO",
|
||||
"propagate": False,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
logging_config_path = tmp_path / "logging_config.json"
|
||||
logging_config_path.write_text(json.dumps(logging_config))
|
||||
|
||||
try:
|
||||
with monkeypatch.context() as context:
|
||||
context.setattr(sys, "stdout", output)
|
||||
# Restore this module-global state when the context exits.
|
||||
context.setattr(vllm_logger, "_vllm_process_info", None)
|
||||
_configure_vllm_root_logger(
|
||||
LoggingConfig(pylogging_config_file=str(logging_config_path))
|
||||
)
|
||||
decorate_logs("Worker_DP0")
|
||||
init_logger("vllm.structured_log_probe").info("structured log probe")
|
||||
|
||||
log = json.loads(output.getvalue())
|
||||
finally:
|
||||
_configure_vllm_root_logger(LoggingConfig())
|
||||
|
||||
assert log["message"] == "structured log probe"
|
||||
assert log["vllm_process_name"] == "Worker_DP0"
|
||||
assert log["process"] == os.getpid()
|
||||
|
||||
|
||||
def test_use_color_force_color(monkeypatch):
|
||||
"""FORCE_COLOR forces colored logs without a TTY, while NO_COLOR and an
|
||||
explicit VLLM_LOGGING_COLOR=0 take precedence over it."""
|
||||
|
||||
+27
-1
@@ -25,11 +25,36 @@ if TYPE_CHECKING:
|
||||
from vllm.config.logging import LoggingConfig
|
||||
|
||||
_FORMAT = (
|
||||
f"{envs.VLLM_LOGGING_PREFIX}%(levelname)s %(asctime)s "
|
||||
f"{envs.VLLM_LOGGING_PREFIX}(%(vllm_process_name)s "
|
||||
"pid=%(process)d) %(levelname)s %(asctime)s "
|
||||
"[%(fileinfo)s:%(lineno)d] %(message)s"
|
||||
)
|
||||
_DATE_FORMAT = "%m-%d %H:%M:%S"
|
||||
|
||||
_base_log_record_factory = logging.getLogRecordFactory()
|
||||
_vllm_process_info: tuple[str, int] | None = None
|
||||
|
||||
|
||||
def _vllm_log_record_factory(*args: Any, **kwargs: Any) -> logging.LogRecord:
|
||||
"""Add vLLM process metadata to each log record."""
|
||||
record = _base_log_record_factory(*args, **kwargs)
|
||||
process_info = _vllm_process_info
|
||||
pid = os.getpid()
|
||||
if process_info is None or process_info[1] != pid:
|
||||
record.vllm_process_name = record.processName
|
||||
else:
|
||||
record.vllm_process_name = process_info[0]
|
||||
return record
|
||||
|
||||
|
||||
def set_vllm_process_name(process_name: str, *, skip_if_set: bool = False) -> None:
|
||||
"""Set the vLLM process name added to subsequent log records."""
|
||||
global _vllm_process_info
|
||||
pid = os.getpid()
|
||||
if skip_if_set and _vllm_process_info is not None and _vllm_process_info[1] == pid:
|
||||
return
|
||||
_vllm_process_info = (process_name, pid)
|
||||
|
||||
|
||||
def _use_color() -> bool:
|
||||
if envs.NO_COLOR or envs.VLLM_LOGGING_COLOR == "0":
|
||||
@@ -195,6 +220,7 @@ def _configure_vllm_root_logger(config: "LoggingConfig | None" = None) -> None:
|
||||
)
|
||||
|
||||
if configure_logging:
|
||||
logging.setLogRecordFactory(_vllm_log_record_factory)
|
||||
logging_config = deepcopy(DEFAULT_LOGGING_CONFIG)
|
||||
|
||||
vllm_handler = logging_config["handlers"]["vllm"]
|
||||
|
||||
@@ -91,11 +91,20 @@ class ColoredFormatter(NewLineFormatter):
|
||||
"CRITICAL": "\033[35m", # Magenta
|
||||
}
|
||||
GREY = "\033[90m" # Grey for timestamp and file info
|
||||
PROCESS_PREFIX_COLOR = "\033[0;36m"
|
||||
PROCESS_PREFIX_RESET = "\033[0;0m"
|
||||
RESET = "\033[0m"
|
||||
|
||||
def __init__(self, fmt, datefmt=None, style="%", *, log_level: str | None = None):
|
||||
# Inject grey color codes into format string for timestamp and file info
|
||||
if fmt:
|
||||
# Wrap process prefix with cyan
|
||||
fmt = fmt.replace(
|
||||
"(%(vllm_process_name)s pid=%(process)d)",
|
||||
f"{self.PROCESS_PREFIX_COLOR}"
|
||||
"(%(vllm_process_name)s pid=%(process)d)"
|
||||
f"{self.PROCESS_PREFIX_RESET}",
|
||||
)
|
||||
# Wrap %(asctime)s with grey
|
||||
fmt = fmt.replace("%(asctime)s", f"{self.GREY}%(asctime)s{self.RESET}")
|
||||
# Wrap [%(fileinfo)s:%(lineno)d] with grey
|
||||
|
||||
@@ -11,12 +11,11 @@ import signal
|
||||
import sys
|
||||
from collections.abc import Callable, Iterator
|
||||
from pathlib import Path
|
||||
from typing import TextIO
|
||||
|
||||
import psutil
|
||||
|
||||
import vllm.envs as envs
|
||||
from vllm.logger import init_logger
|
||||
from vllm.logger import init_logger, set_vllm_process_name
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.platforms.interface import in_wsl
|
||||
from vllm.ray.lazy_utils import is_in_ray_actor
|
||||
@@ -25,10 +24,6 @@ from .platform_utils import cuda_is_initialized, xpu_is_initialized
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
CYAN = "\033[0;36m"
|
||||
RESET = "\033[0;0m"
|
||||
|
||||
|
||||
# Environment variable utilities
|
||||
|
||||
|
||||
@@ -198,59 +193,14 @@ def set_process_title(
|
||||
setproctitle.setproctitle(f"{prefix}::{name}")
|
||||
|
||||
|
||||
def _add_prefix(file: TextIO, worker_name: str, pid: int) -> None:
|
||||
"""Add colored prefix to file output for log decoration."""
|
||||
is_tty = hasattr(file, "isatty") and file.isatty()
|
||||
if (
|
||||
envs.NO_COLOR
|
||||
or envs.VLLM_LOGGING_COLOR == "0"
|
||||
or (envs.VLLM_LOGGING_COLOR != "1" and not is_tty)
|
||||
):
|
||||
prefix = f"({worker_name} pid={pid}) "
|
||||
else:
|
||||
prefix = f"{CYAN}({worker_name} pid={pid}){RESET} "
|
||||
# Use the original write to avoid nesting prefixes on repeated calls.
|
||||
file_write = getattr(file, "_original_write", file.write)
|
||||
|
||||
def write_with_prefix(s: str):
|
||||
if not s:
|
||||
return
|
||||
if file.start_new_line: # type: ignore[attr-defined]
|
||||
file_write(prefix)
|
||||
idx = 0
|
||||
while (next_idx := s.find("\n", idx)) != -1:
|
||||
next_idx += 1
|
||||
file_write(s[idx:next_idx])
|
||||
if next_idx == len(s):
|
||||
file.start_new_line = True # type: ignore[attr-defined]
|
||||
return
|
||||
file_write(prefix)
|
||||
idx = next_idx
|
||||
file_write(s[idx:])
|
||||
file.start_new_line = False # type: ignore[attr-defined]
|
||||
|
||||
file.start_new_line = True # type: ignore[attr-defined]
|
||||
file._original_write = file_write # type: ignore[attr-defined]
|
||||
file.write = write_with_prefix # type: ignore[method-assign]
|
||||
|
||||
|
||||
def decorate_logs(
|
||||
process_name: str | None = None, *, skip_if_decorated: bool = False
|
||||
) -> None:
|
||||
"""Decorate stdout/stderr with process name and PID prefix."""
|
||||
# Respect VLLM_CONFIGURE_LOGGING environment variable
|
||||
if not envs.VLLM_CONFIGURE_LOGGING:
|
||||
return
|
||||
|
||||
if skip_if_decorated and hasattr(sys.stdout, "_original_write"):
|
||||
return
|
||||
|
||||
"""Add vLLM process metadata to subsequent log records."""
|
||||
if process_name is None:
|
||||
process_name = get_mp_context().current_process().name
|
||||
|
||||
pid = os.getpid()
|
||||
_add_prefix(sys.stdout, process_name, pid)
|
||||
_add_prefix(sys.stderr, process_name, pid)
|
||||
set_vllm_process_name(process_name, skip_if_set=skip_if_decorated)
|
||||
|
||||
|
||||
def kill_process_tree(pid: int):
|
||||
|
||||
+1
-1
@@ -530,7 +530,7 @@ def run_api_server_worker_proc(
|
||||
client_config = client_config or {}
|
||||
server_index = client_config.get("client_index", 0)
|
||||
|
||||
# Set process title and add process-specific prefix to stdout and stderr.
|
||||
# Set process title and process-specific log metadata.
|
||||
set_process_title("APIServer", str(server_index))
|
||||
decorate_logs()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user