# src/monitoring/logger.py
"""
Sistema de logging centralizado para todo el bot
"""
from loguru import logger
import sys
from pathlib import Path

def setup_logger(log_level: str = 'INFO'):
    """
    Configura el sistema de logging con rotación y formato
    """
    # Remover handler por defecto
    logger.remove()

    # Console handler con colores
    logger.add(
        sys.stdout,
        format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan> - <level>{message}</level>",
        level=log_level,
        colorize=True,
    )

    # File handler con rotación
    log_path = Path("logs")
    log_path.mkdir(exist_ok=True)

    logger.add(
        log_path / "trading_bot_{time:YYYY-MM-DD}.log",
        format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}",
        level=log_level,
        rotation="500 MB",
        retention="30 days",
        compression="zip"
    )

    # Error file separado
    logger.add(
        log_path / "errors_{time:YYYY-MM-DD}.log",
        format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}",
        level="ERROR",
        rotation="100 MB",
        retention="90 days",
        compression="zip"
    )

    logger.info("Logger inicializado correctamente")
    return logger

# Instancia global
log = setup_logger()