diff --git a/sk/logger.py b/sk/logger.py index 53ea396..d16d486 100644 --- a/sk/logger.py +++ b/sk/logger.py @@ -1,12 +1,18 @@ import logging from loki_logger_handler.loki_logger_handler import LokiLoggerHandler +from uuid import uuid4, UUID LOG_CONSOLE = 1 LOG_FILE = 2 LOG_LOKI = 4 +run_id: UUID # Quick non-intrusive debug check for sk.devops logger def setup(name: str, handlers: int) -> logging.Logger: + global run_id + # Generate a unique run ID. + run_id = uuid4() + logger = logging.getLogger(name) logger.setLevel(logging.DEBUG) @@ -36,9 +42,31 @@ def setup(name: str, handlers: int) -> logging.Logger: return logger +def debug(logger: logging.Logger, message: str, **kwargs): + global run_id + logger.debug(message, extra={"run_id": str(run_id), **kwargs}) + +def info(logger: logging.Logger, message: str, **kwargs): + global run_id + logger.info(message, extra={"run_id": str(run_id), **kwargs}) + +def warning(logger: logging.Logger, message: str, **kwargs): + global run_id + logger.warning(message, extra={"run_id": str(run_id), **kwargs}) + +def error(logger: logging.Logger, message: str, **kwargs): + global run_id + logger.error(message, extra={"run_id": str(run_id), **kwargs}) + +def critical(logger: logging.Logger, message: str, **kwargs): + global run_id + logger.critical(message, extra={"run_id": str(run_id), **kwargs}) + def log_entity_creation(logger: logging.Logger, entity_class: type, entity_key: str): + global run_id logger.debug(f'Created new "{entity_class.__name__}" object with key: "{entity_key}"', extra={ "entity_class": entity_class.__name__, - "entity_key": entity_key + "entity_key": entity_key, + "run_id": str(run_id) })