| import logging | |
| import collections | |
| class GradioLogHandler(logging.Handler): | |
| def __init__(self, log_queue): | |
| super().__init__() | |
| self.log_queue = log_queue | |
| self.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')) | |
| def emit(self, record): | |
| self.log_queue.append(self.format(record)) | |
| def setup_gradio_logging(): | |
| log_queue = collections.deque(maxlen=1000) | |
| gradio_handler = GradioLogHandler(log_queue) | |
| logging.getLogger().setLevel(logging.INFO) | |
| logging.getLogger().addHandler(gradio_handler) | |
| return log_queue, gradio_handler | |