Skip to content

Commit

Permalink
renamed base logger back to 'logger'
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed Jun 19, 2024
1 parent 6f8e7bb commit 4029d56
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
14 changes: 14 additions & 0 deletions xnat_ingest/cli/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@
"mail and log level are delimited by ',' and separate destinations by ';'"
),
)
@click.option(
"--add-logger",
type=str,
multiple=True,
default=(),
envvar="XNAT_INGEST_UPLOAD_LOGGERS",
help=(
"The loggers to use for logging. By default just the 'xnat-ingest' logger is used. "
"But additional loggers can be included (e.g. 'xnat') can be "
"specified here"
),
)
@click.option(
"--mail-server",
type=MailServer.cli_type,
Expand Down Expand Up @@ -173,6 +185,7 @@ def stage(
log_level: str,
log_files: ty.List[LogFile],
log_emails: ty.List[LogEmail],
add_logger: ty.List[str],
mail_server: MailServer,
raise_errors: bool,
deidentify: bool,
Expand All @@ -183,6 +196,7 @@ def stage(
log_emails=log_emails,
log_files=log_files,
mail_server=mail_server,
add_logger=add_logger,
)

if xnat_login:
Expand Down
14 changes: 14 additions & 0 deletions xnat_ingest/cli/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@
"mail and log level are delimited by ',' and separate destinations by ';'"
),
)
@click.option(
"--add-logger",
type=str,
multiple=True,
default=(),
envvar="XNAT_INGEST_UPLOAD_LOGGERS",
help=(
"The loggers to use for logging. By default just the 'xnat-ingest' logger is used. "
"But additional loggers can be included (e.g. 'xnat') can be "
"specified here"
),
)
@click.option(
"--mail-server",
type=MailServer.cli_type,
Expand Down Expand Up @@ -166,6 +178,7 @@ def upload(
log_emails: ty.List[LogEmail],
mail_server: MailServer,
always_include: ty.Sequence[str],
add_logger: ty.List[str],
raise_errors: bool,
store_credentials: ty.Tuple[str, str],
temp_dir: ty.Optional[Path],
Expand All @@ -179,6 +192,7 @@ def upload(
log_emails=log_emails,
log_files=log_files,
mail_server=mail_server,
add_logger=add_logger,
)
if temp_dir:
tempfile.tempdir = str(temp_dir)
Expand Down
14 changes: 8 additions & 6 deletions xnat_ingest/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,10 @@ def test_stage_and_upload(
str(associated_files_dir)
+ "/{PatientName.given_name}_{PatientName.family_name}*.ptd",
r".*/[^\.]+.[^\.]+.[^\.]+.(?P<id>\d+)\.[A-Z]+_(?P<resource>[^\.]+).*",
# "--log-file",
# str(log_file),
# "info",
"--log-file",
str(log_file),
"info",
"--add-logger",
"--raise-errors",
"--delete",
"--xnat-login",
Expand All @@ -214,9 +215,10 @@ def test_stage_and_upload(
upload,
[
str(staging_dir),
# "--log-file",
# str(log_file),
# "info",
"--log-file",
str(log_file),
"info",
"--add-logger",
"--always-include",
"medimage/dicom-series",
"--raise-errors",
Expand Down
23 changes: 17 additions & 6 deletions xnat_ingest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,22 @@ def set_logger_handling(
log_emails: ty.List[LogEmail] | None,
log_files: ty.List[LogFile] | None,
mail_server: MailServer,
add_logger: ty.Sequence[str] = (),
):
loggers = [logger]
for log in add_logger:
loggers.append(logging.getLogger(log))

levels = [log_level]
levels.extend(le.loglevel for le in log_emails)
levels.extend(lf.loglevel for lf in log_files)
if log_emails:
levels.extend(le.loglevel for le in log_emails)
if log_files:
levels.extend(lf.loglevel for lf in log_files)

min_log_level = min(getattr(logging, ll.upper()) for ll in levels)
logger.setLevel(min_log_level)

for logr in loggers:
logr.setLevel(min_log_level)

# Configure the email logger
if log_emails:
Expand All @@ -161,7 +169,8 @@ def set_logger_handling(
secure=None,
)
smtp_hdle.setLevel(getattr(logging, log_email.loglevel.upper()))
logger.addHandler(smtp_hdle)
for logr in loggers:
logr.addHandler(smtp_hdle)

# Configure the file logger
for log_file in log_files:
Expand All @@ -172,14 +181,16 @@ def set_logger_handling(
log_file_hdle.setFormatter(
logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
)
logger.addHandler(log_file_hdle)
for logr in loggers:
logr.addHandler(log_file_hdle)

console_hdle = logging.StreamHandler(sys.stdout)
console_hdle.setLevel(getattr(logging, log_level.upper()))
console_hdle.setFormatter(
logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
)
logger.addHandler(console_hdle)
for logr in loggers:
logr.addHandler(console_hdle)


def get_checksums(xresource) -> ty.Dict[str, str]:
Expand Down

0 comments on commit 4029d56

Please sign in to comment.