Skip to content

Commit 19b13e2

Browse files
authored
Update logging configuration to not when used by a library (#3479)
1 parent 5bc91ce commit 19b13e2

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

src/cfnlint/api.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from typing import List
99

10-
from cfnlint.config import ConfigMixIn, ManualArgs, configure_logging
10+
from cfnlint.config import ConfigMixIn, ManualArgs
1111
from cfnlint.decode.decode import decode_str
1212
from cfnlint.helpers import REGION_PRIMARY, REGIONS
1313
from cfnlint.rules import Match, RulesCollection
@@ -38,7 +38,6 @@ def lint(
3838
list
3939
a list of errors if any were found, else an empty list
4040
"""
41-
configure_logging(None, None)
4241
template, errors = decode_str(s)
4342
if errors:
4443
return errors

src/cfnlint/config.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@
3030

3131
def configure_logging(debug_logging, info_logging):
3232
ch = logging.StreamHandler()
33-
ch.setLevel(logging.DEBUG)
3433

3534
if debug_logging:
3635
LOGGER.setLevel(logging.DEBUG)
3736
elif info_logging:
3837
LOGGER.setLevel(logging.INFO)
3938
else:
40-
LOGGER.setLevel(logging.NOTSET)
39+
LOGGER.setLevel(logging.WARNING)
4140
log_formatter = logging.Formatter(
4241
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
4342
)
@@ -621,7 +620,6 @@ def __init__(self, cli_args: list[str] | None = None, **kwargs: Unpack[ManualArg
621620
self._manual_args = kwargs or ManualArgs()
622621
CliArgs.__init__(self, cli_args)
623622
# configure debug as soon as we can
624-
configure_logging(self.cli_args.debug, self.cli_args.info) # type: ignore
625623
TemplateArgs.__init__(self, {})
626624
ConfigFileArgs.__init__(
627625
self, config_file=self._get_argument_value("config_file", False, False)
@@ -638,6 +636,7 @@ def __repr__(self):
638636
"regions": self.regions,
639637
"ignore_bad_template": self.ignore_bad_template,
640638
"debug": self.debug,
639+
"info": self.info,
641640
"format": self.format,
642641
"templates": self.templates,
643642
"append_rules": self.append_rules,
@@ -717,6 +716,10 @@ def ignore_bad_template(self):
717716
def debug(self):
718717
return self._get_argument_value("debug", False, False)
719718

719+
@property
720+
def info(self):
721+
return self._get_argument_value("info", False, False)
722+
720723
@property
721724
def format(self):
722725
return self._get_argument_value("format", False, True)

src/cfnlint/runner.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import cfnlint.formatters
1515
import cfnlint.maintenance
16-
from cfnlint.config import ConfigMixIn
16+
from cfnlint.config import ConfigMixIn, configure_logging
1717
from cfnlint.decode.decode import decode
1818
from cfnlint.rules import Match, Rules
1919
from cfnlint.rules.errors import ParseError, TransformError
@@ -398,6 +398,9 @@ def cli(self) -> None:
398398
Raises:
399399
None: This function does not raise any exceptions.
400400
"""
401+
# Add our logging configuration when running CLI
402+
configure_logging(self.config.debug, self.config.info)
403+
401404
if self.config.update_specs:
402405
cfnlint.maintenance.update_resource_specs(self.config.force)
403406
sys.exit(0)

test/unit/module/config/test_logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ def test_no_logging(self):
3737
"""Test no logging level"""
3838

3939
cfnlint.config.configure_logging(False, False)
40-
self.assertEqual(logging.NOTSET, LOGGER.level)
40+
self.assertEqual(logging.WARNING, LOGGER.level)
4141
self.assertEqual(len(LOGGER.handlers), 1)

test/unit/module/runner/test_cli.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55

6+
import logging
67
from test.testlib.testcase import BaseTestCase
78
from unittest.mock import patch
89

910
from cfnlint import ConfigMixIn
1011
from cfnlint.runner import Runner
1112

13+
LOGGER = logging.getLogger("cfnlint")
14+
1215

1316
class TestCli(BaseTestCase):
1417
"""Test CLI with config"""
1518

19+
def tearDown(self):
20+
"""Setup"""
21+
for handler in LOGGER.handlers:
22+
LOGGER.removeHandler(handler)
23+
1624
@patch("cfnlint.maintenance.update_documentation")
1725
def test_update_documentation(self, mock_maintenance):
1826
config = ConfigMixIn(["--update-documentation"])

0 commit comments

Comments
 (0)