Spaces:
3SCError404
/
Paused

File size: 2,645 Bytes
5eb4857
 
 
 
 
 
 
 
 
 
 
 
ab8d6bc
 
 
f3f25ca
 
 
 
5eb4857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3f25ca
 
 
 
 
5eb4857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3f25ca
 
 
 
 
5eb4857
 
 
f3f25ca
 
 
 
 
5eb4857
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import logging
import sys
from pathlib import Path
from logging.handlers import TimedRotatingFileHandler
from core.config import get_settings

settings = get_settings()

# Ensure logs directory exists
LOGS_DIR = Path("logs")
LOGS_DIR.mkdir(exist_ok=True)

# Standard logging format used by all handlers
LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"

# Filter to suppress frequent health check logs in production to stay within cloud log quotas
class HealthCheckFilter(logging.Filter):
    def filter(self, record):
        return "/health" not in record.getMessage()

def setup_logging():
    """
    Configure global logging settings for the application.
    """
    log_level = logging.DEBUG if settings.DEBUG else logging.INFO
    
    # Root logger configuration
    root_logger = logging.getLogger()
    root_logger.setLevel(log_level)
    
    # Remove existing handlers to avoid duplicates
    if root_logger.hasHandlers():
        root_logger.handlers.clear()
        
    # 1. Console Handler
    console_handler = logging.StreamHandler(sys.stdout)
    console_handler.setFormatter(logging.Formatter(LOG_FORMAT))
    
    # Add health check filter to console handler if not in DEBUG
    if not settings.DEBUG:
        console_handler.addFilter(HealthCheckFilter())
        
    root_logger.addHandler(console_handler)
    
    # 2. File Handler (Timed Rotating - Daily)
    file_handler = TimedRotatingFileHandler(
        LOGS_DIR / "app.log",
        when="midnight",
        interval=1,
        backupCount=30,  # Keep 30 days of logs
        encoding="utf-8"
    )
    file_handler.setFormatter(logging.Formatter(LOG_FORMAT))
    root_logger.addHandler(file_handler)
    
    # 3. Suppress noisy third-party libraries if not in DEBUG
    if not settings.DEBUG:
        # Keep uvicorn.access at INFO but the filter will catch /health noise
        logging.getLogger("uvicorn.access").setLevel(logging.INFO)
        logging.getLogger("uvicorn.access").addFilter(HealthCheckFilter())
        
        # Suppress heavy debug noise from core libraries
        logging.getLogger("sqlalchemy.engine").setLevel(logging.WARNING)
        logging.getLogger("httpx").setLevel(logging.WARNING)
        logging.getLogger("langchain_ollama").setLevel(logging.INFO)
        logging.getLogger("arq").setLevel(logging.INFO)
        
        # Prevent double logging
        logging.getLogger("uvicorn.access").propagate = False
        logging.getLogger("uvicorn.error").propagate = False

def get_logger(name: str):
    """
    Utility to get a logger instance for a specific module.
    """
    return logging.getLogger(name)