Skip to content

Commit

Permalink
ensure logger pushes to console as well
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthakpati committed Apr 4, 2024
1 parent c68d039 commit 017cb6c
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions ereg/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,7 @@ def register(
if log_file is None:
# TODO this will create trouble for non ".nii.gz" files
log_file = output_image.replace(".nii.gz", ".log")
logging.basicConfig(
filename=log_file,
format="%(asctime)s,%(name)s,%(levelname)s,%(message)s",
datefmt="%H:%M:%S",
level=logging.DEBUG,
)
self.logger = logging.getLogger("registration")
self._setup_logger(log_file, "registration")

self.logger.info(f"Target image: {target_image}, Moving image: {moving_image}")
target_image = read_image_and_cast_to_32bit_float(target_image)
Expand Down Expand Up @@ -241,14 +235,7 @@ def resample_image(
if log_file is None:
# TODO this will create trouble for non ".nii.gz" file
log_file = output_image.replace(".nii.gz", ".log")
reload(logging)
logging.basicConfig(
filename=log_file,
format="%(asctime)s,%(name)s,%(levelname)s,%(message)s",
datefmt="%H:%M:%S",
level=logging.DEBUG,
)
self.logger = logging.getLogger("resample")
self._setup_logger(log_file, "resample")

assert os.path.isfile(transform_file), "Transform file does not exist."
transform_from_file = None
Expand Down Expand Up @@ -815,3 +802,24 @@ def _register_image_and_get_transform(
tmp.SetCenter(registration_transform_sitk.GetCenter())
registration_transform_sitk = tmp
return registration_transform_sitk

def _setup_logger(self, log_file: str, logger_type: str) -> None:
"""
Setup the logger.
Args:
log_file (str): The log file.
logger_type (str): The logger type.
"""
reload(logging)
logging.basicConfig(
filename=log_file,
format="%(asctime)s,%(name)s,%(levelname)s,%(message)s",
datefmt="%H:%M:%S",
level=logging.DEBUG,
)
self.logger = logging.getLogger(logger_type)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# add the handler to the root logger
self.logger.addHandler(console)

0 comments on commit 017cb6c

Please sign in to comment.