Refactor logger functions to include run ID in log messages for better traceability
Some checks failed
/ unit-tests (push) Failing after 6s
Some checks failed
/ unit-tests (push) Failing after 6s
This commit is contained in:
30
sk/logger.py
30
sk/logger.py
@@ -1,12 +1,18 @@
|
|||||||
import logging
|
import logging
|
||||||
from loki_logger_handler.loki_logger_handler import LokiLoggerHandler
|
from loki_logger_handler.loki_logger_handler import LokiLoggerHandler
|
||||||
|
from uuid import uuid4, UUID
|
||||||
|
|
||||||
LOG_CONSOLE = 1
|
LOG_CONSOLE = 1
|
||||||
LOG_FILE = 2
|
LOG_FILE = 2
|
||||||
LOG_LOKI = 4
|
LOG_LOKI = 4
|
||||||
|
run_id: UUID
|
||||||
|
|
||||||
# Quick non-intrusive debug check for sk.devops logger
|
# Quick non-intrusive debug check for sk.devops logger
|
||||||
def setup(name: str, handlers: int) -> logging.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 = logging.getLogger(name)
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
@@ -36,9 +42,31 @@ def setup(name: str, handlers: int) -> logging.Logger:
|
|||||||
|
|
||||||
return 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):
|
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}"',
|
logger.debug(f'Created new "{entity_class.__name__}" object with key: "{entity_key}"',
|
||||||
extra={
|
extra={
|
||||||
"entity_class": entity_class.__name__,
|
"entity_class": entity_class.__name__,
|
||||||
"entity_key": entity_key
|
"entity_key": entity_key,
|
||||||
|
"run_id": str(run_id)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user