Skip to content

Commit

Permalink
logging.py: Don't change log level of the root logger to bigger numer…
Browse files Browse the repository at this point in the history
…ic value
  • Loading branch information
KKoukiou committed Mar 14, 2018
1 parent cbb2c55 commit 4df1a1c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Jurko Gospodnetić
Justyna Janczyszyn
Kale Kundert
Katarzyna Jachim
Katerina Koukiou
Kevin Cox
Kodi B. Arfer
Lawrence Mitchell
Expand Down
2 changes: 1 addition & 1 deletion _pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def catching_logs(handler, formatter=None, level=None):
root_logger.addHandler(handler)
if level is not None:
orig_level = root_logger.level
root_logger.setLevel(level)
root_logger.setLevel(min(orig_level, level))
try:
yield handler
finally:
Expand Down
3 changes: 3 additions & 0 deletions changelog/3307.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pytest should not change the log level of the root logger
when the ``log-level`` parameter is set with value
``LOG_LEVEL>root_logger_level``.
2 changes: 2 additions & 0 deletions doc/en/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ made in ``3.4`` after community feedback:

* Log levels are no longer changed unless explicitly requested by the :confval:`log_level` configuration
or ``--log-level`` command-line options. This allows users to configure logger objects themselves.
When this parameter is used and user requests log level of smaller numeric value than the log level of the root
logger then the root logger will be adjusted to have level equal to the requested logging level.
* :ref:`Live Logs <live_logs>` is now disabled by default and can be enabled setting the
:confval:`log_cli` configuration option to ``true``. When enabled, the verbosity is increased so logging for each
test is visible.
Expand Down
21 changes: 21 additions & 0 deletions testing/logging/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ def test_foo():
'text going to stderr'])


def test_root_logger_affected(testdir):
testdir.makepyfile('''
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.FileHandler('pytest.log'))
def test_foo():
logger.debug('debug text going to logger')
logger.info('info text going to logger')
''')
log_file = testdir.tmpdir.join('pytest.log').strpath
result = testdir.runpytest('--log-level=INFO')
assert os.path.isfile(log_file)
with open(log_file) as rfh:
contents = rfh.read()
assert all(x in contents for x in ["debug text going to logger",
"info text going to logger"])


def test_setup_logging(testdir):
testdir.makepyfile('''
import logging
Expand Down

0 comments on commit 4df1a1c

Please sign in to comment.