Skip to content

Commit

Permalink
Configure root logger instead of package logger (#1773)
Browse files Browse the repository at this point in the history
* Configure root logger instead of package logger

Change:
- This was discussed internally; the goal is to make it so that rules
  which set up their own logger are still subject to the global rules
  of ansible-lint particularly with regard to our verbosity/quietness
  flags.

- To accomodate for using the root logger, the logging levels of -q and
  -qq had to change slightly. This is because the default log level of
  the root logger is WARNING, and we want to avoid setting it to NOTSET
  which would allow everything by default. To work around this, we now
  make our default be WARNING as well. -q becomes ERROR, and -qq
  becomes CRITICAL which we previously didn't have.

Test Plan:
- New integration tests with various levels of -v and -q.

Signed-off-by: Rick Elrod <rick@elrod.me>

* lint

Signed-off-by: Rick Elrod <rick@elrod.me>

* Eliminate some false matches (deprecation warnings)

Signed-off-by: Rick Elrod <rick@elrod.me>
  • Loading branch information
relrod committed Dec 7, 2021
1 parent fd4a746 commit 8c372b6
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,20 @@

def initialize_logger(level: int = 0) -> None:
"""Set up the global logging level based on the verbosity number."""
# We are about to act on the root logger, which defaults to logging.WARNING.
# That is where our 0 (default) value comes from.
VERBOSITY_MAP = {
-2: logging.ERROR,
-1: logging.WARNING,
0: logging.NOTSET,
-2: logging.CRITICAL,
-1: logging.ERROR,
0: logging.WARNING,
1: logging.INFO,
2: logging.DEBUG,
}

handler = logging.StreamHandler()
formatter = logging.Formatter('%(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger = logging.getLogger(__package__)
logger = logging.getLogger()
logger.addHandler(handler)
# Unknown logging level is treated as DEBUG
logging_level = VERBOSITY_MAP.get(level, logging.DEBUG)
Expand Down
98 changes: 98 additions & 0 deletions test/TestVerbosity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"""Tests related to our logging/verbosity setup."""

import os
from typing import List, Tuple

import pytest

from ansiblelint.testing import run_ansible_lint


# substrs is a list of tuples, where:
# component 1 is the substring in question
# component 2 is whether or not to invert ("NOT") the match
@pytest.mark.parametrize(
('verbosity', 'substrs'),
(
(
'',
[
('WARNING Loading custom .yamllint config file,', False),
('WARNING Listing 1 violation(s) that are fatal', False),
('DEBUG ', True),
('INFO ', True),
],
),
(
'-q',
[
('WARNING ', True),
('DEBUG ', True),
('INFO ', True),
],
),
(
'-qq',
[
('WARNING ', True),
('DEBUG ', True),
('INFO ', True),
],
),
(
'-v',
[
('WARNING Loading custom .yamllint config file,', False),
('WARNING Listing 1 violation(s) that are fatal', False),
('INFO Added ANSIBLE_LIBRARY=', False),
('DEBUG ', True),
],
),
(
'-vv',
[
('WARNING Loading custom .yamllint config file,', False),
('WARNING Listing 1 violation(s) that are fatal', False),
('INFO Added ANSIBLE_LIBRARY=', False),
('DEBUG Effective yamllint rules used', False),
],
),
(
'-vvvvvvvvvvvvvvvvvvvvvvvvv',
[
('WARNING Loading custom .yamllint config file,', False),
('WARNING Listing 1 violation(s) that are fatal', False),
('INFO Added ANSIBLE_LIBRARY=', False),
('DEBUG Effective yamllint rules used', False),
],
),
),
ids=(
'default verbosity',
'quiet',
'really quiet',
'loquacious',
'really loquacious',
'really loquacious but with more "v"s -- same as -vv',
),
)
def test_default_verbosity(verbosity: str, substrs: List[Tuple[str, bool]]) -> None:
"""Checks that our default verbosity displays (only) warnings."""
# Piggyback off the .yamllint in the root of the repo, just for testing.
# We'll "override" it with the one in the fixture, to produce a warning.
cwd = os.path.realpath(
os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")
)

fakerole = os.path.join('test', 'fixtures', 'verbosity-tests')

if verbosity:
result = run_ansible_lint(verbosity, fakerole, cwd=cwd)
else:
result = run_ansible_lint(fakerole, cwd=cwd)

for (substr, invert) in substrs:
if invert:
assert substr not in result.stderr, result.stderr
else:
assert substr in result.stderr, result.stderr
1 change: 1 addition & 0 deletions test/fixtures/verbosity-tests/.yamllint
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit 8c372b6

Please sign in to comment.