|
2 | 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
3 | 3 |
|
4 | 4 | import logging |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from vllm import envs |
5 | 8 |
|
6 | 9 |
|
7 | 10 | class NewLineFormatter(logging.Formatter): |
8 | 11 | """Adds logging prefix to newlines to align multi-line messages.""" |
9 | 12 |
|
10 | 13 | def __init__(self, fmt, datefmt=None, style="%"): |
11 | | - logging.Formatter.__init__(self, fmt, datefmt, style) |
| 14 | + super().__init__(fmt, datefmt, style) |
| 15 | + |
| 16 | + self.use_relpath = envs.VLLM_LOGGING_LEVEL == "DEBUG" |
| 17 | + if self.use_relpath: |
| 18 | + self.root_dir = Path(__file__).resolve().parent.parent.parent |
12 | 19 |
|
13 | 20 | def format(self, record): |
14 | | - msg = logging.Formatter.format(self, record) |
| 21 | + |
| 22 | + def shrink_path(relpath: Path) -> str: |
| 23 | + """ |
| 24 | + Shortens a file path for logging display: |
| 25 | + - Removes leading 'vllm' folder if present. |
| 26 | + - If path starts with 'v1', |
| 27 | + keeps the first two and last two levels, |
| 28 | + collapsing the middle as '...'. |
| 29 | + - Otherwise, keeps the first and last two levels, |
| 30 | + collapsing the middle as '...'. |
| 31 | + - If the path is short, returns it as-is. |
| 32 | + - Examples: |
| 33 | + vllm/model_executor/layers/quantization/utils/fp8_utils.py -> |
| 34 | + model_executor/.../quantization/utils/fp8_utils.py |
| 35 | + vllm/model_executor/layers/quantization/awq.py -> |
| 36 | + model_executor/layers/quantization/awq.py |
| 37 | + vllm/v1/attention/backends/mla/common.py -> |
| 38 | + v1/attention/backends/mla/common.py |
| 39 | +
|
| 40 | + Args: |
| 41 | + relpath (Path): The relative path to be shortened. |
| 42 | + Returns: |
| 43 | + str: The shortened path string for display. |
| 44 | + """ |
| 45 | + parts = list(relpath.parts) |
| 46 | + new_parts = [] |
| 47 | + if parts and parts[0] == "vllm": |
| 48 | + parts = parts[1:] |
| 49 | + if parts and parts[0] == "v1": |
| 50 | + new_parts += parts[:2] |
| 51 | + parts = parts[2:] |
| 52 | + elif parts: |
| 53 | + new_parts += parts[:1] |
| 54 | + parts = parts[1:] |
| 55 | + if len(parts) > 2: |
| 56 | + new_parts += ["..."] + parts[-2:] |
| 57 | + else: |
| 58 | + new_parts += parts |
| 59 | + return "/".join(new_parts) |
| 60 | + |
| 61 | + if self.use_relpath: |
| 62 | + abs_path = getattr(record, "pathname", None) |
| 63 | + if abs_path: |
| 64 | + try: |
| 65 | + relpath = Path(abs_path).resolve().relative_to( |
| 66 | + self.root_dir) |
| 67 | + except Exception: |
| 68 | + relpath = Path(record.filename) |
| 69 | + else: |
| 70 | + relpath = Path(record.filename) |
| 71 | + record.fileinfo = shrink_path(relpath) |
| 72 | + else: |
| 73 | + record.fileinfo = record.filename |
| 74 | + |
| 75 | + msg = super().format(record) |
15 | 76 | if record.message != "": |
16 | 77 | parts = msg.split(record.message) |
17 | 78 | msg = msg.replace("\n", "\r\n" + parts[0]) |
|
0 commit comments