from __future__ import annotations import logging from datetime import datetime, timezone from pathlib import Path LOGGER_NAME = "cracklab.client" class UTCFormatter(logging.Formatter): converter = staticmethod( lambda timestamp: datetime.fromtimestamp( timestamp, tz=timezone.utc, ).timetuple() ) def _configure( name: str, log_path: Path, ) -> logging.Logger: log_path.parent.mkdir(parents=True, exist_ok=True) logger = logging.getLogger(name) logger.setLevel(logging.INFO) logger.propagate = False if logger.handlers: return logger handler = logging.FileHandler( log_path, mode="a", encoding="utf-8", ) handler.setFormatter( UTCFormatter( "%(asctime)sZ %(levelname)s %(message)s", datefmt="%Y-%m-%dT%H:%M:%S", ) ) logger.addHandler(handler) return logger def configure_logging(log_path: Path) -> logging.Logger: return _configure( LOGGER_NAME, log_path, ) def configure_job_logging( log_path: Path, job_id: str, ) -> logging.Logger: return _configure( f"{LOGGER_NAME}.{job_id}", log_path, )