diff --git a/src/polyphy/utils/logger.py b/src/polyphy/utils/logger.py index dbbfe78..98b5517 100644 --- a/src/polyphy/utils/logger.py +++ b/src/polyphy/utils/logger.py @@ -6,6 +6,8 @@ from datetime import datetime import logging import time +import traceback +from logging.handlers import RotatingFileHandler class Logger: @@ -45,6 +47,7 @@ def logToStdOut(level, *msg) -> None: # FILE: file_logger = logging.getLogger("file") file_handler = logging.FileHandler("polyphy.log") + file_handler = RotatingFileHandler("polyphy.log", maxBytes=1024*1024, backupCount=3) file_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") file_handler.setFormatter(file_formatter) file_logger.addHandler(file_handler) @@ -61,3 +64,13 @@ def logToFile(level, *msg) -> None: Logger.file_logger.setLevel(Logger.log_level.get(level, logging.INFO)) res = " ".join(map(str, msg)) Logger.file_log_functions.get(level, Logger.file_logger.info)(res) + + @staticmethod + def logException(level, exception, *msg) -> None: + log_msg = " ".join(map(str, msg)) + log_msg += f"\nException: {repr(exception)}" + log_msg += f"\nStack Trace: {traceback.format_exc()}" + # Log to console + Logger.logToStdOut(level, log_msg) + # Log to file + Logger.logToFile(level, log_msg)