[Core][Logging] Fix JSON logging process decoration (#57957)
Signed-off-by: Mark McLoughlin <markmc@redhat.com>
This commit is contained in:
+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