File size: 1,177 Bytes
f6ea7de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
from loguru import logger
import time
from pathlib import Path

def setup_api_logging(log_path: Path ):
    """
    Configure Loguru logger for the API
    
    Args:
        log_path: Optional path to store log files
    """
    # Remove default handler
    logger.remove()
    
    # Add stdout handler with custom format
    logger.add(
        sys.stdout,
        colorize=True,
        format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
        level="INFO"
    )
    
    # Add file logging if path provided
    if log_path:
        log_path.parent.mkdir(parents=True, exist_ok=True)
        logger.add(
            log_path / f"api_{time.strftime('%Y%m%d')}.log",
            rotation="00:00",  # New file at midnight
            compression="zip",  # Compress old logs
            retention="30 days",  # Keep logs for 30 days
            format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}",
            level="DEBUG"
        )
    
    logger.info("API logging configured")
    
    return logger