From 9ace3e84235642d0ff54ac4d92339c2dbd773dbf Mon Sep 17 00:00:00 2001 From: Kevin DeJong Date: Mon, 8 Jul 2024 12:13:53 -0700 Subject: [PATCH] Update logging configuration to not when used by a library --- src/cfnlint/api.py | 3 +-- src/cfnlint/config.py | 9 ++++++--- src/cfnlint/runner.py | 5 ++++- test/unit/module/config/test_logging.py | 2 +- test/unit/module/runner/test_cli.py | 8 ++++++++ 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/cfnlint/api.py b/src/cfnlint/api.py index 70cfd30003..78c67f805c 100644 --- a/src/cfnlint/api.py +++ b/src/cfnlint/api.py @@ -7,7 +7,7 @@ from typing import List -from cfnlint.config import ConfigMixIn, ManualArgs, configure_logging +from cfnlint.config import ConfigMixIn, ManualArgs from cfnlint.decode.decode import decode_str from cfnlint.helpers import REGION_PRIMARY, REGIONS from cfnlint.rules import Match, RulesCollection @@ -38,7 +38,6 @@ def lint( list a list of errors if any were found, else an empty list """ - configure_logging(None, None) template, errors = decode_str(s) if errors: return errors diff --git a/src/cfnlint/config.py b/src/cfnlint/config.py index 5ecee1ec2b..fd8934e39e 100644 --- a/src/cfnlint/config.py +++ b/src/cfnlint/config.py @@ -30,14 +30,13 @@ def configure_logging(debug_logging, info_logging): ch = logging.StreamHandler() - ch.setLevel(logging.DEBUG) if debug_logging: LOGGER.setLevel(logging.DEBUG) elif info_logging: LOGGER.setLevel(logging.INFO) else: - LOGGER.setLevel(logging.NOTSET) + LOGGER.setLevel(logging.WARNING) log_formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s" ) @@ -621,7 +620,6 @@ def __init__(self, cli_args: list[str] | None = None, **kwargs: Unpack[ManualArg self._manual_args = kwargs or ManualArgs() CliArgs.__init__(self, cli_args) # configure debug as soon as we can - configure_logging(self.cli_args.debug, self.cli_args.info) # type: ignore TemplateArgs.__init__(self, {}) ConfigFileArgs.__init__( self, config_file=self._get_argument_value("config_file", False, False) @@ -638,6 +636,7 @@ def __repr__(self): "regions": self.regions, "ignore_bad_template": self.ignore_bad_template, "debug": self.debug, + "info": self.info, "format": self.format, "templates": self.templates, "append_rules": self.append_rules, @@ -717,6 +716,10 @@ def ignore_bad_template(self): def debug(self): return self._get_argument_value("debug", False, False) + @property + def info(self): + return self._get_argument_value("info", False, False) + @property def format(self): return self._get_argument_value("format", False, True) diff --git a/src/cfnlint/runner.py b/src/cfnlint/runner.py index 54b5aeac3b..f2ba7ab070 100644 --- a/src/cfnlint/runner.py +++ b/src/cfnlint/runner.py @@ -13,7 +13,7 @@ import cfnlint.formatters import cfnlint.maintenance -from cfnlint.config import ConfigMixIn +from cfnlint.config import ConfigMixIn, configure_logging from cfnlint.decode.decode import decode from cfnlint.rules import Match, Rules from cfnlint.rules.errors import ParseError, TransformError @@ -398,6 +398,9 @@ def cli(self) -> None: Raises: None: This function does not raise any exceptions. """ + # Add our logging configuration when running CLI + configure_logging(self.config.debug, self.config.info) + if self.config.update_specs: cfnlint.maintenance.update_resource_specs(self.config.force) sys.exit(0) diff --git a/test/unit/module/config/test_logging.py b/test/unit/module/config/test_logging.py index d8c0259bb2..5a32737327 100644 --- a/test/unit/module/config/test_logging.py +++ b/test/unit/module/config/test_logging.py @@ -37,5 +37,5 @@ def test_no_logging(self): """Test no logging level""" cfnlint.config.configure_logging(False, False) - self.assertEqual(logging.NOTSET, LOGGER.level) + self.assertEqual(logging.WARNING, LOGGER.level) self.assertEqual(len(LOGGER.handlers), 1) diff --git a/test/unit/module/runner/test_cli.py b/test/unit/module/runner/test_cli.py index ac9831a731..5d3f3f7244 100644 --- a/test/unit/module/runner/test_cli.py +++ b/test/unit/module/runner/test_cli.py @@ -3,16 +3,24 @@ SPDX-License-Identifier: MIT-0 """ +import logging from test.testlib.testcase import BaseTestCase from unittest.mock import patch from cfnlint import ConfigMixIn from cfnlint.runner import Runner +LOGGER = logging.getLogger("cfnlint") + class TestCli(BaseTestCase): """Test CLI with config""" + def tearDown(self): + """Setup""" + for handler in LOGGER.handlers: + LOGGER.removeHandler(handler) + @patch("cfnlint.maintenance.update_documentation") def test_update_documentation(self, mock_maintenance): config = ConfigMixIn(["--update-documentation"])