Skip to content

Commit 8c372b6

Browse files
authored
Configure root logger instead of package logger (#1773)
* 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 <[email protected]> * lint Signed-off-by: Rick Elrod <[email protected]> * Eliminate some false matches (deprecation warnings) Signed-off-by: Rick Elrod <[email protected]>
1 parent fd4a746 commit 8c372b6

File tree

3 files changed

+105
-4
lines changed

3 files changed

+105
-4
lines changed

src/ansiblelint/__main__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,20 @@
5858

5959
def initialize_logger(level: int = 0) -> None:
6060
"""Set up the global logging level based on the verbosity number."""
61+
# We are about to act on the root logger, which defaults to logging.WARNING.
62+
# That is where our 0 (default) value comes from.
6163
VERBOSITY_MAP = {
62-
-2: logging.ERROR,
63-
-1: logging.WARNING,
64-
0: logging.NOTSET,
64+
-2: logging.CRITICAL,
65+
-1: logging.ERROR,
66+
0: logging.WARNING,
6567
1: logging.INFO,
6668
2: logging.DEBUG,
6769
}
6870

6971
handler = logging.StreamHandler()
7072
formatter = logging.Formatter('%(levelname)-8s %(message)s')
7173
handler.setFormatter(formatter)
72-
logger = logging.getLogger(__package__)
74+
logger = logging.getLogger()
7375
logger.addHandler(handler)
7476
# Unknown logging level is treated as DEBUG
7577
logging_level = VERBOSITY_MAP.get(level, logging.DEBUG)

test/TestVerbosity.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""Tests related to our logging/verbosity setup."""
2+
3+
import os
4+
from typing import List, Tuple
5+
6+
import pytest
7+
8+
from ansiblelint.testing import run_ansible_lint
9+
10+
11+
# substrs is a list of tuples, where:
12+
# component 1 is the substring in question
13+
# component 2 is whether or not to invert ("NOT") the match
14+
@pytest.mark.parametrize(
15+
('verbosity', 'substrs'),
16+
(
17+
(
18+
'',
19+
[
20+
('WARNING Loading custom .yamllint config file,', False),
21+
('WARNING Listing 1 violation(s) that are fatal', False),
22+
('DEBUG ', True),
23+
('INFO ', True),
24+
],
25+
),
26+
(
27+
'-q',
28+
[
29+
('WARNING ', True),
30+
('DEBUG ', True),
31+
('INFO ', True),
32+
],
33+
),
34+
(
35+
'-qq',
36+
[
37+
('WARNING ', True),
38+
('DEBUG ', True),
39+
('INFO ', True),
40+
],
41+
),
42+
(
43+
'-v',
44+
[
45+
('WARNING Loading custom .yamllint config file,', False),
46+
('WARNING Listing 1 violation(s) that are fatal', False),
47+
('INFO Added ANSIBLE_LIBRARY=', False),
48+
('DEBUG ', True),
49+
],
50+
),
51+
(
52+
'-vv',
53+
[
54+
('WARNING Loading custom .yamllint config file,', False),
55+
('WARNING Listing 1 violation(s) that are fatal', False),
56+
('INFO Added ANSIBLE_LIBRARY=', False),
57+
('DEBUG Effective yamllint rules used', False),
58+
],
59+
),
60+
(
61+
'-vvvvvvvvvvvvvvvvvvvvvvvvv',
62+
[
63+
('WARNING Loading custom .yamllint config file,', False),
64+
('WARNING Listing 1 violation(s) that are fatal', False),
65+
('INFO Added ANSIBLE_LIBRARY=', False),
66+
('DEBUG Effective yamllint rules used', False),
67+
],
68+
),
69+
),
70+
ids=(
71+
'default verbosity',
72+
'quiet',
73+
'really quiet',
74+
'loquacious',
75+
'really loquacious',
76+
'really loquacious but with more "v"s -- same as -vv',
77+
),
78+
)
79+
def test_default_verbosity(verbosity: str, substrs: List[Tuple[str, bool]]) -> None:
80+
"""Checks that our default verbosity displays (only) warnings."""
81+
# Piggyback off the .yamllint in the root of the repo, just for testing.
82+
# We'll "override" it with the one in the fixture, to produce a warning.
83+
cwd = os.path.realpath(
84+
os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")
85+
)
86+
87+
fakerole = os.path.join('test', 'fixtures', 'verbosity-tests')
88+
89+
if verbosity:
90+
result = run_ansible_lint(verbosity, fakerole, cwd=cwd)
91+
else:
92+
result = run_ansible_lint(fakerole, cwd=cwd)
93+
94+
for (substr, invert) in substrs:
95+
if invert:
96+
assert substr not in result.stderr, result.stderr
97+
else:
98+
assert substr in result.stderr, result.stderr
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)