From 1011c7d024136cfe11beaf915d73c6bf522cafaf Mon Sep 17 00:00:00 2001 From: Tim Fennell Date: Fri, 15 Nov 2024 14:36:07 -0700 Subject: [PATCH] Make sure logger.getLogger() is used instead of logging directly via logging.debug() (#91) Ensures that anywhere that `prymer` logs it is using a non-root logger. --- prymer/api/picking.py | 1 + prymer/api/variant_lookup.py | 8 +++++--- prymer/primer3/primer3.py | 2 +- prymer/primer3/primer3_failure_reason.py | 2 +- prymer/util/executable_runner.py | 12 ++++++++---- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/prymer/api/picking.py b/prymer/api/picking.py index c4a0536..e78da2e 100644 --- a/prymer/api/picking.py +++ b/prymer/api/picking.py @@ -16,6 +16,7 @@ from individual left and primers. """ + from collections.abc import Sequence from pathlib import Path from typing import Iterator diff --git a/prymer/api/variant_lookup.py b/prymer/api/variant_lookup.py index 74794e5..a08a6a4 100644 --- a/prymer/api/variant_lookup.py +++ b/prymer/api/variant_lookup.py @@ -80,6 +80,8 @@ from prymer.api.span import Span from prymer.api.span import Strand +_logger = logging.getLogger(__name__) + @unique class VariantType(UppercaseStrEnum): @@ -282,7 +284,7 @@ def query( variants = self._query(refname=refname, start=start, end=end) if len(variants) == 0: - logging.debug(f"No variants extracted from region of interest: {refname}:{start}-{end}") + _logger.debug(f"No variants extracted from region of interest: {refname}:{start}-{end}") if maf is None or maf <= 0.0: return variants elif include_missing_mafs: # return variants with a MAF above threshold or missing @@ -311,7 +313,7 @@ def to_variants( ): # if passing or empty filters simple_variants = SimpleVariant.build(variant) if any(v.variant_type == VariantType.OTHER for v in simple_variants): - logging.debug( + _logger.debug( f"Input VCF file {source_vcf} may contain complex variants: {variant}" ) simple_vars.extend(simple_variants) @@ -372,7 +374,7 @@ def _query(self, refname: str, start: int, end: int) -> list[SimpleVariant]: simple_variants: list[SimpleVariant] = [] for fh, path in zip(self._readers, self.vcf_paths, strict=True): if not fh.header.contigs.get(refname): - logging.debug(f"Header in VCF file {path} does not contain chromosome {refname}.") + _logger.debug(f"Header in VCF file {path} does not contain chromosome {refname}.") continue # pysam.fetch is 0-based, half-open variants = [variant for variant in fh.fetch(contig=refname, start=start - 1, end=end)] diff --git a/prymer/primer3/primer3.py b/prymer/primer3/primer3.py index 97bddbc..7dd2b92 100644 --- a/prymer/primer3/primer3.py +++ b/prymer/primer3/primer3.py @@ -268,7 +268,7 @@ def close(self) -> bool: self._fasta.close() subprocess_close = super().close() if not subprocess_close: - logging.debug("Did not successfully close underlying subprocess") + logging.getLogger(__name__).debug("Did not successfully close underlying subprocess") return subprocess_close def get_design_sequences(self, region: Span) -> tuple[str, str]: diff --git a/prymer/primer3/primer3_failure_reason.py b/prymer/primer3/primer3_failure_reason.py index 071549e..a46957e 100644 --- a/prymer/primer3/primer3_failure_reason.py +++ b/prymer/primer3/primer3_failure_reason.py @@ -112,7 +112,7 @@ def parse_failures( continue std_reason = Primer3FailureReason.from_reason(reason) if std_reason is None: - logging.debug(f"Unknown Primer3 failure reason: {reason}") + logging.getLogger(__name__).debug(f"Unknown Primer3 failure reason: {reason}") by_fail_count[std_reason] += count return by_fail_count diff --git a/prymer/util/executable_runner.py b/prymer/util/executable_runner.py index 26da723..d806a86 100644 --- a/prymer/util/executable_runner.py +++ b/prymer/util/executable_runner.py @@ -70,7 +70,9 @@ def __init__( ) def __enter__(self) -> Self: - logging.debug(f"Initiating {self._name} with the following params: {self._command}") + logging.getLogger(__name__).debug( + f"Initiating {self._name} with the following params: {self._command}" + ) return self def __exit__( @@ -142,15 +144,17 @@ def close(self) -> bool: True: if the subprocess was terminated successfully False: if the subprocess failed to terminate or was not already running """ + log = logging.getLogger(__name__) + if self.is_alive: self._subprocess.terminate() self._subprocess.wait(timeout=10) if not self.is_alive: - logging.debug("Subprocess terminated successfully.") + log.debug("Subprocess terminated successfully.") return True else: - logging.debug("Subprocess failed to terminate.") + log.debug("Subprocess failed to terminate.") return False else: - logging.debug("Subprocess is not running.") + log.debug("Subprocess is not running.") return False