Skip to content

Commit

Permalink
set up logging levels correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed Dec 11, 2023
1 parent 47b0d0d commit 1e95c0f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
21 changes: 21 additions & 0 deletions scripts/logging_testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from click.testing import CliRunner
from xnat_ingest.cli.stage import stage
from xnat_ingest.utils import show_cli_trace

runner = CliRunner()
result = runner.invoke(
stage,
[
"/Users/tclose/.medimages4tests/cache/dicom/mri/t1w/siemens/skyra/syngo_d13c",
"/Users/tclose/test-stage",
"--project-id",
"TESTSTAGE",
"--log-level",
"INFO"
],
catch_exceptions=False
)

assert result.exit_code == 0, show_cli_trace(result)

print(result.stdout)
5 changes: 3 additions & 2 deletions xnat_ingest/cli/stage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pathlib import Path
import typing as ty
import traceback
import click
from tqdm import tqdm
Expand Down Expand Up @@ -144,8 +145,8 @@ def stage(
project_id: str | None,
delete: bool,
log_level: str,
log_file: Path,
log_emails: LogEmail,
log_file: LogFile | None,
log_emails: ty.List[LogEmail],
mail_server: MailServer,
raise_errors: bool,
):
Expand Down
12 changes: 11 additions & 1 deletion xnat_ingest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,18 @@ class AssociatedFiles(CliType):


def set_logger_handling(
log_level: str, log_emails: LogEmail, log_file: LogFile, mail_server: MailServer
log_level: str, log_emails: ty.List[LogEmail] | None, log_file: LogFile | None, mail_server: MailServer
):

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

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

# Configure the email logger
if log_emails:
if not mail_server:
Expand Down

0 comments on commit 1e95c0f

Please sign in to comment.