Skip to content

Commit 2962c73

Browse files
authored
Merge pull request #3307 from KKoukiou/rhv-qe-fix
logging.py: Don't change log level of the root logger to bigger numeric value
2 parents ed118d7 + 6cfed00 commit 2962c73

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Jurko Gospodnetić
106106
Justyna Janczyszyn
107107
Kale Kundert
108108
Katarzyna Jachim
109+
Katerina Koukiou
109110
Kevin Cox
110111
Kodi B. Arfer
111112
Kostis Anagnostopoulos

_pytest/logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def catching_logs(handler, formatter=None, level=None):
153153
root_logger.addHandler(handler)
154154
if level is not None:
155155
orig_level = root_logger.level
156-
root_logger.setLevel(level)
156+
root_logger.setLevel(min(orig_level, level))
157157
try:
158158
yield handler
159159
finally:

changelog/3307.feature.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pytest not longer changes the log level of the root logger when the
2+
``log-level`` parameter has greater numeric value than that of the level of
3+
the root logger, which makes it play better with custom logging configuration in user code.

testing/logging/test_reporting.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,66 @@ def test_foo():
4949
'text going to stderr'])
5050

5151

52+
def test_root_logger_affected(testdir):
53+
testdir.makepyfile("""
54+
import logging
55+
logger = logging.getLogger()
56+
def test_foo():
57+
logger.info('info text ' + 'going to logger')
58+
logger.warning('warning text ' + 'going to logger')
59+
logger.error('error text ' + 'going to logger')
60+
61+
assert 0
62+
""")
63+
log_file = testdir.tmpdir.join('pytest.log').strpath
64+
result = testdir.runpytest('--log-level=ERROR', '--log-file=pytest.log')
65+
assert result.ret == 1
66+
67+
# the capture log calls in the stdout section only contain the
68+
# logger.error msg, because --log-level=ERROR
69+
result.stdout.fnmatch_lines(['*error text going to logger*'])
70+
with pytest.raises(pytest.fail.Exception):
71+
result.stdout.fnmatch_lines(['*warning text going to logger*'])
72+
with pytest.raises(pytest.fail.Exception):
73+
result.stdout.fnmatch_lines(['*info text going to logger*'])
74+
75+
# the log file should contain the warning and the error log messages and
76+
# not the info one, because the default level of the root logger is
77+
# WARNING.
78+
assert os.path.isfile(log_file)
79+
with open(log_file) as rfh:
80+
contents = rfh.read()
81+
assert "info text going to logger" not in contents
82+
assert "warning text going to logger" in contents
83+
assert "error text going to logger" in contents
84+
85+
86+
def test_log_cli_level_log_level_interaction(testdir):
87+
testdir.makepyfile("""
88+
import logging
89+
logger = logging.getLogger()
90+
91+
def test_foo():
92+
logger.debug('debug text ' + 'going to logger')
93+
logger.info('info text ' + 'going to logger')
94+
logger.warning('warning text ' + 'going to logger')
95+
logger.error('error text ' + 'going to logger')
96+
assert 0
97+
""")
98+
99+
result = testdir.runpytest('--log-cli-level=INFO', '--log-level=ERROR')
100+
assert result.ret == 1
101+
102+
result.stdout.fnmatch_lines([
103+
'*-- live log call --*',
104+
'*INFO*info text going to logger',
105+
'*WARNING*warning text going to logger',
106+
'*ERROR*error text going to logger',
107+
'=* 1 failed in *=',
108+
])
109+
assert 'DEBUG' not in result.stdout.str()
110+
111+
52112
def test_setup_logging(testdir):
53113
testdir.makepyfile('''
54114
import logging
@@ -61,7 +121,7 @@ def setup_function(function):
61121
def test_foo():
62122
logger.info('text going to logger from call')
63123
assert False
64-
''')
124+
''')
65125
result = testdir.runpytest('--log-level=INFO')
66126
assert result.ret == 1
67127
result.stdout.fnmatch_lines(['*- Captured *log setup -*',

0 commit comments

Comments
 (0)