|
4 | 4 | import logging
|
5 | 5 | import os
|
6 | 6 | import sys
|
7 |
| -from functools import partial |
| 7 | +from functools import lru_cache, partial |
8 | 8 | from logging import Logger
|
9 | 9 | from logging.config import dictConfig
|
10 | 10 | from os import path
|
11 |
| -from typing import Dict, Optional |
| 11 | +from types import MethodType |
| 12 | +from typing import Any, Optional, cast |
12 | 13 |
|
13 | 14 | import vllm.envs as envs
|
14 | 15 |
|
|
49 | 50 | }
|
50 | 51 |
|
51 | 52 |
|
| 53 | +@lru_cache |
| 54 | +def _print_info_once(logger: Logger, msg: str) -> None: |
| 55 | + # Set the stacklevel to 2 to print the original caller's line info |
| 56 | + logger.info(msg, stacklevel=2) |
| 57 | + |
| 58 | + |
| 59 | +@lru_cache |
| 60 | +def _print_warning_once(logger: Logger, msg: str) -> None: |
| 61 | + # Set the stacklevel to 2 to print the original caller's line info |
| 62 | + logger.warning(msg, stacklevel=2) |
| 63 | + |
| 64 | + |
| 65 | +class _VllmLogger(Logger): |
| 66 | + """ |
| 67 | + Note: |
| 68 | + This class is just to provide type information. |
| 69 | + We actually patch the methods directly on the :class:`logging.Logger` |
| 70 | + instance to avoid conflicting with other libraries such as |
| 71 | + `intel_extension_for_pytorch.utils._logger`. |
| 72 | + """ |
| 73 | + |
| 74 | + def info_once(self, msg: str) -> None: |
| 75 | + """ |
| 76 | + As :meth:`info`, but subsequent calls with the same message |
| 77 | + are silently dropped. |
| 78 | + """ |
| 79 | + _print_info_once(self, msg) |
| 80 | + |
| 81 | + def warning_once(self, msg: str) -> None: |
| 82 | + """ |
| 83 | + As :meth:`warning`, but subsequent calls with the same message |
| 84 | + are silently dropped. |
| 85 | + """ |
| 86 | + _print_warning_once(self, msg) |
| 87 | + |
| 88 | + |
52 | 89 | def _configure_vllm_root_logger() -> None:
|
53 |
| - logging_config: Dict = {} |
| 90 | + logging_config = dict[str, Any]() |
54 | 91 |
|
55 | 92 | if not VLLM_CONFIGURE_LOGGING and VLLM_LOGGING_CONFIG_PATH:
|
56 | 93 | raise RuntimeError(
|
@@ -84,12 +121,22 @@ def _configure_vllm_root_logger() -> None:
|
84 | 121 | dictConfig(logging_config)
|
85 | 122 |
|
86 | 123 |
|
87 |
| -def init_logger(name: str) -> Logger: |
| 124 | +def init_logger(name: str) -> _VllmLogger: |
88 | 125 | """The main purpose of this function is to ensure that loggers are
|
89 | 126 | retrieved in such a way that we can be sure the root vllm logger has
|
90 | 127 | already been configured."""
|
91 | 128 |
|
92 |
| - return logging.getLogger(name) |
| 129 | + logger = logging.getLogger(name) |
| 130 | + |
| 131 | + methods_to_patch = { |
| 132 | + "info_once": _print_info_once, |
| 133 | + "warning_once": _print_warning_once, |
| 134 | + } |
| 135 | + |
| 136 | + for method_name, method in methods_to_patch.items(): |
| 137 | + setattr(logger, method_name, MethodType(method, logger)) |
| 138 | + |
| 139 | + return cast(_VllmLogger, logger) |
93 | 140 |
|
94 | 141 |
|
95 | 142 | # The root logger is initialized when the module is imported.
|
|
0 commit comments