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})