Skip to content

Commit

Permalink
Update logging configuration to not when used by a library (#3479)
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong authored Jul 8, 2024
1 parent 5bc91ce commit 19b13e2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/cfnlint/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/cfnlint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion src/cfnlint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/unit/module/config/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 8 additions & 0 deletions test/unit/module/runner/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down

0 comments on commit 19b13e2

Please sign in to comment.