File size: 808 Bytes
e3b27f8
 
0ed44b6
 
18f26ab
 
 
0ed44b6
e3b27f8
 
 
 
0ed44b6
e3b27f8
 
 
 
 
18f26ab
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import logging

class ChatIDLoggerAdapter(logging.LoggerAdapter):
    def process(self, msg, kwargs):
        # Include the chat_id in the log message if provided
        chat_id = self.extra.get('chat_id', 'N/A')
        return f"[Chat ID: {chat_id}] {msg}", kwargs

def setup_logger(name, chat_id=None):
    logger = logging.getLogger(name)
    if not logger.handlers:
        logger.setLevel(logging.INFO)
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        
        # Console handler
        console_handler = logging.StreamHandler()
        console_handler.setFormatter(formatter)
        logger.addHandler(console_handler)
    
    # Ensure we return a LoggerAdapter with or without chat_id
    return ChatIDLoggerAdapter(logger, {'chat_id': chat_id})