chat / utils /logging_config.py
Paul Magee
Comprehensive UI/UX and Architecture Improvements
c05de71
raw
history blame
1.47 kB
"""
Logging configuration for the chatbot application.
"""
import logging
import sys
import os
from datetime import datetime
import config
def setup_logging(log_to_file=False):
"""Configure logging for the application.
Args:
log_to_file: Whether to log to a file in addition to console
"""
# Get log level from config
log_level_str = config.LOG_LEVEL
log_level = getattr(logging, log_level_str.upper(), logging.INFO)
# Configure root logger
logging.basicConfig(
level=log_level,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[logging.StreamHandler(sys.stdout)]
)
# Add file handler if requested
if log_to_file:
# Create logs directory if it doesn't exist
if not os.path.exists('logs'):
os.makedirs('logs')
# Create a file handler
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
file_handler = logging.FileHandler(f'logs/chatbot_{timestamp}.log')
file_handler.setLevel(log_level)
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
# Add file handler to root logger
logging.getLogger().addHandler(file_handler)
# Disable other noisy loggers
logging.getLogger('httpx').setLevel(logging.WARNING)
# Return logger for the application
return logging.getLogger('chatbot')