"csrc/vscode:/vscode.git/clone" did not exist on "fc911880cc505197a8eaa54d0d9c49edfa593b92"
logger.py 5.75 KB
Newer Older
1
"""Logging configuration for vLLM."""
2
import datetime
3
import json
Woosuk Kwon's avatar
Woosuk Kwon committed
4
import logging
5
import os
6
import sys
7
from functools import partial
8
9
10
11
from logging import Logger
from logging.config import dictConfig
from os import path
from typing import Dict, Optional
Woosuk Kwon's avatar
Woosuk Kwon committed
12

13
14
15
16
import vllm.envs as envs

VLLM_CONFIGURE_LOGGING = envs.VLLM_CONFIGURE_LOGGING
VLLM_LOGGING_CONFIG_PATH = envs.VLLM_LOGGING_CONFIG_PATH
17
VLLM_LOGGING_LEVEL = envs.VLLM_LOGGING_LEVEL
18
VLLM_LOGGING_PREFIX = envs.VLLM_LOGGING_PREFIX
19

20
21
_FORMAT = (f"{VLLM_LOGGING_PREFIX}%(levelname)s %(asctime)s "
           "%(filename)s:%(lineno)d] %(message)s")
Woosuk Kwon's avatar
Woosuk Kwon committed
22
23
_DATE_FORMAT = "%m-%d %H:%M:%S"

24
25
26
DEFAULT_LOGGING_CONFIG = {
    "formatters": {
        "vllm": {
27
            "class": "vllm.logging_utils.NewLineFormatter",
28
29
30
31
32
33
34
35
            "datefmt": _DATE_FORMAT,
            "format": _FORMAT,
        },
    },
    "handlers": {
        "vllm": {
            "class": "logging.StreamHandler",
            "formatter": "vllm",
36
            "level": VLLM_LOGGING_LEVEL,
37
38
39
40
41
42
43
44
45
46
47
            "stream": "ext://sys.stdout",
        },
    },
    "loggers": {
        "vllm": {
            "handlers": ["vllm"],
            "level": "DEBUG",
            "propagate": False,
        },
    },
    "version": 1,
48
    "disable_existing_loggers": False
49
50
51
52
}


def _configure_vllm_root_logger() -> None:
53
    logging_config: Dict = {}
54
55
56
57
58
59
60

    if not VLLM_CONFIGURE_LOGGING and VLLM_LOGGING_CONFIG_PATH:
        raise RuntimeError(
            "VLLM_CONFIGURE_LOGGING evaluated to false, but "
            "VLLM_LOGGING_CONFIG_PATH was given. VLLM_LOGGING_CONFIG_PATH "
            "implies VLLM_CONFIGURE_LOGGING. Please enable "
            "VLLM_CONFIGURE_LOGGING or unset VLLM_LOGGING_CONFIG_PATH.")
Woosuk Kwon's avatar
Woosuk Kwon committed
61

62
63
    if VLLM_CONFIGURE_LOGGING:
        logging_config = DEFAULT_LOGGING_CONFIG
Woosuk Kwon's avatar
Woosuk Kwon committed
64

65
66
67
68
69
    if VLLM_LOGGING_CONFIG_PATH:
        if not path.exists(VLLM_LOGGING_CONFIG_PATH):
            raise RuntimeError(
                "Could not load logging config. File does not exist: %s",
                VLLM_LOGGING_CONFIG_PATH)
70
        with open(VLLM_LOGGING_CONFIG_PATH, encoding="utf-8") as file:
71
            custom_config = json.loads(file.read())
Woosuk Kwon's avatar
Woosuk Kwon committed
72

73
74
75
76
        if not isinstance(custom_config, dict):
            raise ValueError("Invalid logging config. Expected Dict, got %s.",
                             type(custom_config).__name__)
        logging_config = custom_config
Woosuk Kwon's avatar
Woosuk Kwon committed
77

78
79
80
81
82
    for formatter in logging_config.get("formatters", {}).values():
        # This provides backwards compatibility after #10134.
        if formatter.get("class") == "vllm.logging.NewLineFormatter":
            formatter["class"] = "vllm.logging_utils.NewLineFormatter"

83
84
    if logging_config:
        dictConfig(logging_config)
Woosuk Kwon's avatar
Woosuk Kwon committed
85
86


87
88
89
90
def init_logger(name: str) -> Logger:
    """The main purpose of this function is to ensure that loggers are
    retrieved in such a way that we can be sure the root vllm logger has
    already been configured."""
Woosuk Kwon's avatar
Woosuk Kwon committed
91

92
    return logging.getLogger(name)
Woosuk Kwon's avatar
Woosuk Kwon committed
93
94


95
# The root logger is initialized when the module is imported.
Woosuk Kwon's avatar
Woosuk Kwon committed
96
97
# This is thread-safe as the module is only imported once,
# guaranteed by the Python GIL.
98
_configure_vllm_root_logger()
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

logger = init_logger(__name__)


def _trace_calls(log_path, root_dir, frame, event, arg=None):
    if event in ['call', 'return']:
        # Extract the filename, line number, function name, and the code object
        filename = frame.f_code.co_filename
        lineno = frame.f_lineno
        func_name = frame.f_code.co_name
        if not filename.startswith(root_dir):
            # only log the functions in the vllm root_dir
            return
        # Log every function call or return
        try:
114
115
116
117
118
119
120
121
122
123
            last_frame = frame.f_back
            if last_frame is not None:
                last_filename = last_frame.f_code.co_filename
                last_lineno = last_frame.f_lineno
                last_func_name = last_frame.f_code.co_name
            else:
                # initial frame
                last_filename = ""
                last_lineno = 0
                last_func_name = ""
124
            with open(log_path, 'a') as f:
125
                ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
126
                if event == 'call':
127
                    f.write(f"{ts} Call to"
128
129
130
                            f" {func_name} in {filename}:{lineno}"
                            f" from {last_func_name} in {last_filename}:"
                            f"{last_lineno}\n")
131
                else:
132
                    f.write(f"{ts} Return from"
133
134
135
                            f" {func_name} in {filename}:{lineno}"
                            f" to {last_func_name} in {last_filename}:"
                            f"{last_lineno}\n")
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
        except NameError:
            # modules are deleted during shutdown
            pass
    return partial(_trace_calls, log_path, root_dir)


def enable_trace_function_call(log_file_path: str,
                               root_dir: Optional[str] = None):
    """
    Enable tracing of every function call in code under `root_dir`.
    This is useful for debugging hangs or crashes.
    `log_file_path` is the path to the log file.
    `root_dir` is the root directory of the code to trace. If None, it is the
    vllm root directory.

    Note that this call is thread-level, any threads calling this function
    will have the trace enabled. Other threads will not be affected.
    """
    logger.warning(
        "VLLM_TRACE_FUNCTION is enabled. It will record every"
        " function executed by Python. This will slow down the code. It "
        "is suggested to be used for debugging hang or crashes only.")
158
    logger.info("Trace frame log is saved to %s", log_file_path)
159
160
161
162
    if root_dir is None:
        # by default, this is the vllm root directory
        root_dir = os.path.dirname(os.path.dirname(__file__))
    sys.settrace(partial(_trace_calls, log_file_path, root_dir))