From 1e744d6136a4848a606621b35b1d7524a563623b Mon Sep 17 00:00:00 2001 From: Andy Xie Date: Fri, 14 Nov 2025 22:58:45 +0800 Subject: [PATCH] rename file info to import package file Signed-off-by: Andy Xie --- vllm/envs.py | 7 ++++ vllm/logging_utils/formatter.py | 66 ++++++--------------------------- 2 files changed, 18 insertions(+), 55 deletions(-) diff --git a/vllm/envs.py b/vllm/envs.py index 0530938c32f9e..6c4e4ce24e8c7 100755 --- a/vllm/envs.py +++ b/vllm/envs.py @@ -39,6 +39,7 @@ if TYPE_CHECKING: VLLM_USAGE_SOURCE: str = "" VLLM_CONFIGURE_LOGGING: int = 1 VLLM_LOGGING_LEVEL: str = "INFO" + VLLM_LOGGING_IMPORT_PACKAGE_FILE: bool = False VLLM_LOGGING_PREFIX: str = "" VLLM_LOGGING_STREAM: str = "ext://sys.stdout" VLLM_LOGGING_CONFIG_PATH: str | None = None @@ -604,6 +605,12 @@ environment_variables: dict[str, Callable[[], Any]] = { "VLLM_LOGGING_CONFIG_PATH": lambda: os.getenv("VLLM_LOGGING_CONFIG_PATH"), # this is used for configuring the default logging level "VLLM_LOGGING_LEVEL": lambda: os.getenv("VLLM_LOGGING_LEVEL", "INFO").upper(), + # If set to 0, vllm will only print file name in logging record + # If set to 1, vllm will print import package file name in logging record + "VLLM_LOGGING_IMPORT_PACKAGE_FILE": lambda: os.environ.get( + "VLLM_LOGGING_IMPORT_PACKAGE_FILE", "0" + ) + == "1", # this is used for configuring the default logging stream "VLLM_LOGGING_STREAM": lambda: os.getenv("VLLM_LOGGING_STREAM", "ext://sys.stdout"), # if set, VLLM_LOGGING_PREFIX will be prepended to all log messages diff --git a/vllm/logging_utils/formatter.py b/vllm/logging_utils/formatter.py index 02ba308e18796..9bcfc76ed64c1 100644 --- a/vllm/logging_utils/formatter.py +++ b/vllm/logging_utils/formatter.py @@ -2,7 +2,7 @@ # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import logging -from pathlib import Path +import os from vllm import envs @@ -13,62 +13,18 @@ class NewLineFormatter(logging.Formatter): def __init__(self, fmt, datefmt=None, style="%"): super().__init__(fmt, datefmt, style) - self.use_relpath = envs.VLLM_LOGGING_LEVEL == "DEBUG" - if self.use_relpath: - self.root_dir = Path(__file__).resolve().parent.parent.parent - def format(self, record): - def shrink_path(relpath: Path) -> str: - """ - Shortens a file path for logging display: - - Removes leading 'vllm' folder if present. - - If path starts with 'v1', - keeps the first two and last two levels, - collapsing the middle as '...'. - - Otherwise, keeps the first and last two levels, - collapsing the middle as '...'. - - If the path is short, returns it as-is. - - Examples: - vllm/model_executor/layers/quantization/utils/fp8_utils.py -> - model_executor/.../quantization/utils/fp8_utils.py - vllm/model_executor/layers/quantization/awq.py -> - model_executor/layers/quantization/awq.py - vllm/v1/attention/backends/mla/common.py -> - v1/attention/backends/mla/common.py + filename = getattr(record, "filename", None) + assert filename is not None + name = getattr(record, "name", None) - Args: - relpath (Path): The relative path to be shortened. - Returns: - str: The shortened path string for display. - """ - parts = list(relpath.parts) - new_parts = [] - if parts and parts[0] == "vllm": - parts = parts[1:] - if parts and parts[0] == "v1": - new_parts += parts[:2] - parts = parts[2:] - elif parts: - new_parts += parts[:1] - parts = parts[1:] - if len(parts) > 2: - new_parts += ["..."] + parts[-2:] - else: - new_parts += parts - return "/".join(new_parts) - - if self.use_relpath: - abs_path = getattr(record, "pathname", None) - if abs_path: - try: - relpath = Path(abs_path).resolve().relative_to(self.root_dir) - except Exception: - relpath = Path(record.filename) - else: - relpath = Path(record.filename) - record.fileinfo = shrink_path(relpath) - else: - record.fileinfo = record.filename + record.fileinfo = filename + if envs.VLLM_LOGGING_IMPORT_PACKAGE_FILE and name is not None: + # assume logger name is the package name, ie, __name__ + parts = name.split(".") + if filename != "__init__.py": + parts = parts[:-1] + record.fileinfo = os.path.join(*parts, filename) msg = super().format(record) if record.message != "":