From 9a05096f376198ccc60aac281c8f512cb0da9d7c Mon Sep 17 00:00:00 2001 From: Sharon Fitzpatrick Date: Wed, 29 Nov 2023 18:08:00 -0800 Subject: [PATCH] release logger handler once logging is done --- src/coastsat/SDS_download.py | 23 +++++++++++++++++++---- src/coastsat/SDS_shoreline.py | 4 +++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/coastsat/SDS_download.py b/src/coastsat/SDS_download.py index e3616b0..c0342e8 100644 --- a/src/coastsat/SDS_download.py +++ b/src/coastsat/SDS_download.py @@ -44,8 +44,25 @@ np.seterr(all="ignore") # raise/ignore divisions by 0 and nans gdal.PushErrorHandler("CPLQuietErrorHandler") +def release_logger(logger): + """ + Release the logger and its associated file handlers. -def setup_logger(folder, base_filename="download_report",log_format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"): + :param logger: The logger object to be released. + """ + # Remove all handlers associated with the logger + for handler in logger.handlers[:]: + # Close the handler if it's a FileHandler + if isinstance(handler, logging.FileHandler): + handler.close() + # Remove the handler from the logger + logger.removeHandler(handler) + +def setup_logger( + folder, + base_filename="download_report", + log_format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", +): # Determine the next available log file number i = 1 while True: @@ -64,9 +81,7 @@ def setup_logger(folder, base_filename="download_report",log_format="%(asctime)s file_handler.setLevel(logging.INFO) # Create formatters and add it to handlers - log_format = logging.Formatter( - log_format - ) + log_format = logging.Formatter(log_format) file_handler.setFormatter(log_format) # Add handlers to the logger diff --git a/src/coastsat/SDS_shoreline.py b/src/coastsat/SDS_shoreline.py index fecd100..4e4e615 100644 --- a/src/coastsat/SDS_shoreline.py +++ b/src/coastsat/SDS_shoreline.py @@ -49,7 +49,7 @@ np.seterr(all="ignore") # raise/ignore divisions by 0 and nans -from coastsat.SDS_download import setup_logger +from coastsat.SDS_download import setup_logger, release_logger # Main function for batch shoreline detection @@ -410,6 +410,8 @@ def extract_shorelines( filepath = os.path.join(filepath_data, sitename) json_path = os.path.join(filepath, sitename + "_output.json") SDS_preprocess.write_to_json(json_path, output) + # release the logger as it is no longer needed + release_logger(logger) return output