From 0d0285a296c7bd113ce6a654f78b6a2f329b7b75 Mon Sep 17 00:00:00 2001 From: plgmlesniak Date: Tue, 15 Oct 2024 08:38:55 +0200 Subject: [PATCH 1/2] ISEPOS-2280 Adjusted log file name in comment --- python/logging/base_logger.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/logging/base_logger.py b/python/logging/base_logger.py index b4234d8..a0f7067 100644 --- a/python/logging/base_logger.py +++ b/python/logging/base_logger.py @@ -11,7 +11,7 @@ def getDefaultLogger(name): Retrieves or creates a logger with the specified name and sets it up with a file handler. The logger is configured to write log messages to the file path specified by the - 'DEFAULT_LOG_PATH' environment variable. It uses the 'INFO' level as the default + 'APP_LOG_FILE' environment variable. It uses the 'INFO' level as the default logging level and writes log entries in the following format: 'YYYY-MM-DD HH:MM:SS,ms LEVEL logger_name message' @@ -30,13 +30,13 @@ def getDefaultLogger(name): -------- logger : logging.Logger A logger instance with the specified name. The logger is configured with a - file handler that writes to the file specified by the 'DEFAULT_LOG_PATH' + file handler that writes to the file specified by the 'APP_LOG_FILE' environment variable. Raises: ------- KeyError: - If the 'DEFAULT_LOG_PATH' environment variable is not set. + If the 'APP_LOG_FILE' environment variable is not set. Example: -------- -- 2.16.5 From 13e4fa808f24664fab0aca0e78fd7a59c1d109f3 Mon Sep 17 00:00:00 2001 From: plgmlesniak Date: Tue, 15 Oct 2024 11:55:59 +0200 Subject: [PATCH 2/2] ISEPOS-2280 Added fallback paths for base loggers --- matlab/logging/base_logger.m | 6 +++++- octave/logging/base_logger.m | 6 +++++- python/logging/base_logger.py | 21 ++++++++++++--------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/matlab/logging/base_logger.m b/matlab/logging/base_logger.m index b98d68c..5fffdca 100644 --- a/matlab/logging/base_logger.m +++ b/matlab/logging/base_logger.m @@ -63,7 +63,11 @@ classdef base_logger < handle methods(Access=private) function this = base_logger() - this.fid = fopen(getenv("APP_LOG_FILE"), 'a'); + logFileName = getenv("APP_LOG_FILE"); + if isempty(logFileName) + logFileName = 'fallbackPathApplication.log'; + end + this.fid = fopen(logFileName, 'a'); if this.fid == -1 error('Failed to open log file'); end diff --git a/octave/logging/base_logger.m b/octave/logging/base_logger.m index 70f62fc..7e86da9 100644 --- a/octave/logging/base_logger.m +++ b/octave/logging/base_logger.m @@ -63,7 +63,11 @@ classdef base_logger < handle methods function this = base_logger() - this.fid = fopen(getenv("APP_LOG_FILE"), 'a'); + logFileName = getenv("APP_LOG_FILE"); + if isempty(logFileName) + logFileName = 'fallbackPathApplication.log'; + end + this.fid = fopen(logFileName, 'a'); if this.fid == -1 error('Failed to open log file'); end diff --git a/python/logging/base_logger.py b/python/logging/base_logger.py index a0f7067..16f6f0b 100644 --- a/python/logging/base_logger.py +++ b/python/logging/base_logger.py @@ -11,8 +11,10 @@ def getDefaultLogger(name): Retrieves or creates a logger with the specified name and sets it up with a file handler. The logger is configured to write log messages to the file path specified by the - 'APP_LOG_FILE' environment variable. It uses the 'INFO' level as the default - logging level and writes log entries in the following format: + 'APP_LOG_FILE' environment variable. If the environment variable is not set, + the logger will write to the file 'fallbackPathApplication.log' in the current + working directory. The logger uses the 'INFO' level as the default logging level + and writes log entries in the following format: 'YYYY-MM-DD HH:MM:SS,ms LEVEL logger_name message' @@ -31,12 +33,8 @@ def getDefaultLogger(name): logger : logging.Logger A logger instance with the specified name. The logger is configured with a file handler that writes to the file specified by the 'APP_LOG_FILE' - environment variable. - - Raises: - ------- - KeyError: - If the 'APP_LOG_FILE' environment variable is not set. + environment variable, or to 'fallbackPathApplication.log' if the environment + variable is not set. Example: -------- @@ -47,10 +45,15 @@ def getDefaultLogger(name): except Exception: logger.exception('An error occurred') + Notes: + ------ + - The 'APP_LOG_FILE' environment variable should specify the full path to the log file. + - If 'APP_LOG_FILE' is not set, logs will be written to 'fallbackPathApplication.log'. + """ logger = logging.getLogger(name) if not logger.hasHandlers(): - file_handler = logging.FileHandler(os.environ['APP_LOG_FILE'], mode='a') + file_handler = logging.FileHandler(os.environ.get('APP_LOG_FILE', 'fallbackPathApplication.log'), mode='a') formatter = logging.Formatter('%(asctime)s,%(msecs)d %(levelname)s %(name)s %(message)s') file_handler.setFormatter(formatter) logger.addHandler(file_handler) -- 2.16.5