Source code for trinity.utils.log

# Adapted from
# https://github.com/skypilot-org/skypilot/blob/86dc0f6283a335e4aa37b3c10716f90999f48ab6/sky/sky_logging.py
"""Logging configuration for vLLM."""
import logging
import sys

_FORMAT = "%(levelname)s %(asctime)s %(filename)s:%(lineno)d] %(message)s"
_DATE_FORMAT = "%m-%d %H:%M:%S"


[docs] class NewLineFormatter(logging.Formatter): """Adds logging prefix to newlines to align multi-line messages."""
[docs] def __init__(self, fmt, datefmt=None): logging.Formatter.__init__(self, fmt, datefmt)
[docs] def format(self, record): msg = logging.Formatter.format(self, record) if record.message != "": parts = msg.split(record.message) msg = msg.replace("\n", "\r\n" + parts[0]) return msg
_root_logger = logging.getLogger("trinity") _default_handler = None def _setup_logger(): _root_logger.setLevel(logging.DEBUG) global _default_handler if _default_handler is None: _default_handler = logging.StreamHandler(sys.stdout) _default_handler.flush = sys.stdout.flush # type: ignore _default_handler.setLevel(logging.INFO) _root_logger.addHandler(_default_handler) fmt = NewLineFormatter(_FORMAT, datefmt=_DATE_FORMAT) _default_handler.setFormatter(fmt) # Setting this will avoid the message # being propagated to the parent logger. _root_logger.propagate = False # The logger is initialized when the module is imported. # This is thread-safe as the module is only imported once, # guaranteed by the Python GIL. _setup_logger()
[docs] def get_logger(name: str, level: int = logging.DEBUG) -> logging.Logger: """Get a logger with the given name and level. Args: name (str): The name of the logger. level (int, optional): The level of the logger. Defaults to logging.DEBUG. Returns: logging.Logger: The logger with the given name and level. """ logger = logging.getLogger(name) logger.setLevel(level) if not logger.handlers: logger.addHandler(_default_handler) # type: ignore [arg-type] logger.propagate = False return logger