From ecd0c9d1cd0e88284299fecc6416f5f01cadf10d Mon Sep 17 00:00:00 2001 From: Ben Thorner Date: Thu, 18 Nov 2021 08:20:46 +0000 Subject: [PATCH] Move logger config to CLI module Previously we configured the root logger in the PMS package itself, which would mean any code using the package as a library would get the logging config as a side effect. Rather than configure the root logger, which should be left to the toplevel program, this re-scopes how we set the config to apply to just the specific logger for the package. --- src/pms/__init__.py | 2 -- src/pms/cli.py | 8 ++++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/pms/__init__.py b/src/pms/__init__.py index b374b40..4549e10 100644 --- a/src/pms/__init__.py +++ b/src/pms/__init__.py @@ -1,7 +1,5 @@ import logging -import os -logging.basicConfig(level=os.getenv("LEVEL", "WARNING")) logger = logging.getLogger(__name__) diff --git a/src/pms/cli.py b/src/pms/cli.py index a5cfc8d..7428deb 100644 --- a/src/pms/cli.py +++ b/src/pms/cli.py @@ -1,3 +1,5 @@ +import logging +import os import sys from datetime import datetime from enum import Enum @@ -25,6 +27,9 @@ main.command(name=ep.name)(ep.load()) +logging.basicConfig(level=os.getenv("LEVEL", "WARNING")) + + def version_callback(value: bool): # pragma: no cover if not value: return @@ -45,8 +50,7 @@ def callback( version: Optional[bool] = Option(None, "--version", "-V", callback=version_callback), ): """Read serial sensor""" - if debug: # pragma: no cover - logger.setLevel("DEBUG") + logger.setLevel("DEBUG" if debug else os.getenv("LEVEL", "WARNING")) ctx.obj = {"reader": SensorReader(model, port, seconds, samples)}