From 1f254730fcf866171b3fdc57f377aab338c53fd2 Mon Sep 17 00:00:00 2001 From: Brian Rutledge Date: Sun, 23 Jan 2022 14:34:50 -0500 Subject: [PATCH] Use dictConfig --- twine/cli.py | 36 +++++++++++++++++++++--------------- twine/settings.py | 4 ++-- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/twine/cli.py b/twine/cli.py index 801a9a37..dba77713 100644 --- a/twine/cli.py +++ b/twine/cli.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import argparse -import logging +import logging.config from typing import Any, List, Tuple import importlib_metadata @@ -48,20 +48,26 @@ def configure_logging() -> None: - root_logger = logging.getLogger("twine") - - # This prevents failures test_main.py due to capsys not being cleared. - # TODO: Use dictConfig() instead? - for handler in root_logger.handlers: - root_logger.removeHandler(handler) - - root_logger.addHandler( - rich.logging.RichHandler( - console=console, - show_time=False, - show_path=False, - highlighter=rich.highlighter.NullHighlighter(), - ) + # Using dictConfig to override existing loggers, which prevents failures in + # test_main.py due to capsys not being cleared. + logging.config.dictConfig( + { + "version": 1, + "handlers": { + "console": { + "class": "rich.logging.RichHandler", + "console": console, + "show_time": False, + "show_path": False, + "highlighter": rich.highlighter.NullHighlighter(), + } + }, + "loggers": { + "twine": { + "handlers": ["console"], + }, + }, + } ) diff --git a/twine/settings.py b/twine/settings.py index 7c89e771..7ea5d4b1 100644 --- a/twine/settings.py +++ b/twine/settings.py @@ -149,8 +149,8 @@ def verbose(self) -> bool: def verbose(self, verbose: bool) -> None: """Initialize a logger based on the --verbose option.""" self._verbose = verbose - root_logger = logging.getLogger("twine") - root_logger.setLevel(logging.INFO if verbose else logging.WARNING) + twine_logger = logging.getLogger("twine") + twine_logger.setLevel(logging.INFO if verbose else logging.WARNING) @staticmethod def register_argparse_arguments(parser: argparse.ArgumentParser) -> None: