Skip to content

Commit b5fb300

Browse files
authored
[Log] Use a relative path in debug-level logs to distinguish files with identical names (#23846)
Signed-off-by: zjy0516 <[email protected]>
1 parent 15de5ff commit b5fb300

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

vllm/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
VLLM_LOGGING_PREFIX = envs.VLLM_LOGGING_PREFIX
2323

2424
_FORMAT = (f"{VLLM_LOGGING_PREFIX}%(levelname)s %(asctime)s "
25-
"[%(filename)s:%(lineno)d] %(message)s")
25+
"[%(fileinfo)s:%(lineno)d] %(message)s")
2626
_DATE_FORMAT = "%m-%d %H:%M:%S"
2727

2828
DEFAULT_LOGGING_CONFIG = {

vllm/logging_utils/formatter.py

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,77 @@
22
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
33

44
import logging
5+
from pathlib import Path
6+
7+
from vllm import envs
58

69

710
class NewLineFormatter(logging.Formatter):
811
"""Adds logging prefix to newlines to align multi-line messages."""
912

1013
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
1219

1320
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)
1576
if record.message != "":
1677
parts = msg.split(record.message)
1778
msg = msg.replace("\n", "\r\n" + parts[0])

0 commit comments

Comments
 (0)