Spaces:
Sleeping
Sleeping
import logging | |
class ListHandler(logging.Handler): | |
"""Logging handler that appends log messages to an internal list (for in-memory capture).""" | |
def __init__(self, log_list): | |
super().__init__() | |
self.log_list = log_list | |
# Use a simple formatter with time, level, and message | |
formatter = logging.Formatter(fmt="%(asctime)s - %(levelname)s - %(message)s", | |
datefmt="%H:%M:%S") | |
self.setFormatter(formatter) | |
def emit(self, record): | |
log_entry = self.format(record) | |
self.log_list.append(log_entry) | |
def get_log_handler(): | |
""" | |
Create a ListHandler and return (handler, log_list). | |
This allows capturing logs in a list for a specific operation. | |
""" | |
log_list = [] | |
handler = ListHandler(log_list) | |
handler.setLevel(logging.DEBUG) | |
return handler, log_list | |