test24 / api /logger.py
Niansuh's picture
Update api/logger.py
1990d11 verified
raw
history blame
774 Bytes
import logging
class ChatIDLoggerAdapter(logging.LoggerAdapter):
def process(self, msg, kwargs):
# Check if 'chat_id' is passed in kwargs; otherwise, use 'N/A'
chat_id = kwargs.pop('chat_id', 'N/A')
return f"[Chat ID: {chat_id}] {msg}", kwargs
def setup_logger(name):
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)
# Return the LoggerAdapter without a fixed chat_id
return ChatIDLoggerAdapter(logger, {})