File size: 885 Bytes
bb68eb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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