import logging
from logging.handlers import RotatingFileHandler
from pathlib import Path

# Глобальная настройка логирования
def configure_global_logging(
    log_file="logs/app.log",
    level=logging.INFO,
    max_bytes=5_000_000,
    backup_count=3
):
    log_file_path = Path(log_file)
    log_file_path.parent.mkdir(parents=True, exist_ok=True)

    # Основной логгер
    logger = logging.getLogger()
    logger.setLevel(level)

    if not logger.hasHandlers():
        # Обработчик для файла
        file_handler = RotatingFileHandler(
            log_file, maxBytes=max_bytes, backupCount=backup_count
        )
        file_handler.setLevel(level)
        formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
        file_handler.setFormatter(formatter)
        logger.addHandler(file_handler)

        # Обработчик для консоли
        console_handler = logging.StreamHandler()
        console_handler.setLevel(level)
        console_handler.setFormatter(formatter)
        logger.addHandler(console_handler)

    return logger


