File size: 1,092 Bytes
e7abd9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
import logging
import sys
from tqdm import tqdm

def get_tqdm_handler():
    """
    Creates a special handler for tqdm that doesn't interfere with other logs.
    """
    class TqdmLoggingHandler(logging.Handler):
        def emit(self, record):
            try:
                msg = self.format(record)
                tqdm.write(msg)
                self.flush()
            except Exception:
                self.handleError(record)

    return TqdmLoggingHandler()

def setup_service_logger(service_name: str) -> logging.Logger:
    """
    Configure a specific logger for a given service.
    """
    logger = logging.getLogger(f"app.services.{service_name}")
    
    # If the logger already has handlers, don't reconfigure it
    if logger.handlers:
        return logger
    
    # Add tqdm handler for this service
    tqdm_handler = get_tqdm_handler()
    tqdm_handler.setFormatter(logging.Formatter('%(name)s - %(levelname)s - %(message)s'))
    logger.addHandler(tqdm_handler)
    
    # Don't propagate logs to parent loggers
    logger.propagate = False
    
    return logger