Skip to content

Commit

Permalink
Update logging configuration to not when used by a library
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong committed Jul 8, 2024
1 parent 5bc91ce commit c207f61
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 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
6 changes: 5 additions & 1 deletion src/cfnlint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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)
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
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 c207f61

Please sign in to comment.