Skip to content

Commit

Permalink
release logger handler once logging is done
Browse files Browse the repository at this point in the history
  • Loading branch information
2320sharon committed Nov 30, 2023
1 parent 80d4121 commit 9a05096
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
23 changes: 19 additions & 4 deletions src/coastsat/SDS_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/coastsat/SDS_shoreline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 9a05096

Please sign in to comment.