From c207f619e70200178cfbd03819daf2c9406e026b 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 | 6 +++++- src/cfnlint/runner.py | 5 ++++- test/unit/module/runner/test_cli.py | 8 ++++++++ 4 files changed, 18 insertions(+), 4 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..978fe6eb42 100644 --- a/src/cfnlint/config.py +++ b/src/cfnlint/config.py @@ -621,7 +621,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 +637,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 +717,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/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"])