@contextlib.contextmanager
def capture_logs():
    logger = LOGGER
    messages = []

    def emit(record):
        message = '%s: %s' % (record.levelname.upper(), record.getMessage())
        messages.append(message)

    previous_handlers = logger.handlers
    logger.handlers = []
    logger.addHandler(CallbackHandler(emit))
    try:
        yield messages
    finally:
        logger.handlers = previous_handlers
Return a context manager that captures all logged messages.
dataset/ETHPy150Open Kozea/WeasyPrint/weasyprint/tests/testing_utils.py/capture_logs