def capture_logs(logger='weasyprint', level=None):
    """Return a context manager that captures all logged messages."""
    if level is None:
        level = logging.INFO
    logger = logging.getLogger(logger)
    messages = []

    def emit(record):
        if record.name == 'weasyprint.progress':
            return
        if record.levelno < level:
            return
        messages.append(f'{record.levelname.upper()}: {record.getMessage()}')

    previous_handlers = logger.handlers
    previous_level = logger.level
    logger.handlers = []
    logger.addHandler(CallbackHandler(emit))
    logger.setLevel(logging.DEBUG)
    try:
        yield messages
    finally:
        logger.handlers = previous_handlers
        logger.setLevel(previous_level)
Return a context manager that captures all logged messages.
weasyprint/logger.py