CoolFace
Apppublic

BioinstLab/gmass-demo

sourceHugging Faceapache-2.0updated 2d agoView on Hugging Face
0likes
logger.py55 linesDownload Raw Back to core
1"""2logger.py - rotating logger for G-MASS evaluation runs.3MediSafe-GH · Biomedical Technologies Lab4"""5 6import logging7import os8from logging.handlers import RotatingFileHandler9 10LOG_DIR = os.getenv("GMASS_LOG_DIR", "logs")11LOG_FILE = os.path.join(LOG_DIR, "gmass_eval.log")12LOG_FMT = "%(asctime)s [%(levelname)s] %(name)s - %(message)s"13DATE_FMT = "%Y-%m-%dT%H:%M:%S"14 15 16def get_logger(name: str) -> logging.Logger:17    """18    Return a named logger that writes to both console and a rotating log file.19    Safe to call multiple times with the same name - returns the same logger.20 21    Args:22        name: typically __name__ of the calling module.23 24    Returns:25        Configured logging.Logger instance.26    """27    logger = logging.getLogger(name)28 29    if logger.handlers:30        return logger31 32    logger.setLevel(logging.DEBUG)33    formatter = logging.Formatter(LOG_FMT, datefmt=DATE_FMT)34 35    console = logging.StreamHandler()36    console.setLevel(logging.INFO)37    console.setFormatter(formatter)38    logger.addHandler(console)39 40    try:41        os.makedirs(LOG_DIR, exist_ok=True)42        file_handler = RotatingFileHandler(43            LOG_FILE,44            maxBytes=5 * 1024 * 1024,45            backupCount=3,46            encoding="utf-8",47        )48        file_handler.setLevel(logging.DEBUG)49        file_handler.setFormatter(formatter)50        logger.addHandler(file_handler)51    except OSError:52        logger.warning(f"Could not create log file at {LOG_FILE}. Logging to console only.")53 54    return logger55