File size: 1,466 Bytes
c05de71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
"""
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')