Skip to content

Commit e5f754a

Browse files
committed
Update logging docs with the new changes in 3.4
Ref: #3013
1 parent bdf87f2 commit e5f754a

File tree

1 file changed

+69
-24
lines changed

1 file changed

+69
-24
lines changed

doc/en/logging.rst

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,8 @@
33
Logging
44
-------
55

6-
.. versionadded 3.3.0
7-
8-
.. note::
9-
10-
This feature is a drop-in replacement for the `pytest-catchlog
11-
<https://pypi.org/project/pytest-catchlog/>`_ plugin and they will conflict
12-
with each other. The backward compatibility API with ``pytest-capturelog``
13-
has been dropped when this feature was introduced, so if for that reason you
14-
still need ``pytest-catchlog`` you can disable the internal feature by
15-
adding to your ``pytest.ini``:
16-
17-
.. code-block:: ini
18-
19-
[pytest]
20-
addopts=-p no:logging
6+
.. versionadded:: 3.3
7+
.. versionchanged:: 3.4
218

229
Log messages are captured by default and for each failed test will be shown in
2310
the same manner as captured stdout and stderr.
@@ -29,7 +16,7 @@ Running without options::
2916
Shows failed tests like so::
3017

3118
----------------------- Captured stdlog call ----------------------
32-
test_reporting.py 26 INFO text going to logger
19+
test_reporting.py 26 WARNING text going to logger
3320
----------------------- Captured stdout call ----------------------
3421
text going to stdout
3522
----------------------- Captured stderr call ----------------------
@@ -49,7 +36,7 @@ Running pytest specifying formatting options::
4936
Shows failed tests like so::
5037

5138
----------------------- Captured stdlog call ----------------------
52-
2010-04-10 14:48:44 INFO text going to logger
39+
2010-04-10 14:48:44 WARNING text going to logger
5340
----------------------- Captured stdout call ----------------------
5441
text going to stdout
5542
----------------------- Captured stderr call ----------------------
@@ -92,30 +79,32 @@ messages. This is supported by the ``caplog`` fixture::
9279
caplog.set_level(logging.INFO)
9380
pass
9481

95-
By default the level is set on the handler used to catch the log messages,
82+
By default the level is set on the root logger,
9683
however as a convenience it is also possible to set the log level of any
9784
logger::
9885

9986
def test_foo(caplog):
10087
caplog.set_level(logging.CRITICAL, logger='root.baz')
10188
pass
10289

90+
The log levels set are restored automatically at the end of the test.
91+
10392
It is also possible to use a context manager to temporarily change the log
104-
level::
93+
level inside a ``with`` block::
10594

10695
def test_bar(caplog):
10796
with caplog.at_level(logging.INFO):
10897
pass
10998

110-
Again, by default the level of the handler is affected but the level of any
99+
Again, by default the level of the root logger is affected but the level of any
111100
logger can be changed instead with::
112101

113102
def test_bar(caplog):
114103
with caplog.at_level(logging.CRITICAL, logger='root.baz'):
115104
pass
116105

117106
Lastly all the logs sent to the logger during the test run are made available on
118-
the fixture in the form of both the LogRecord instances and the final log text.
107+
the fixture in the form of both the ``logging.LogRecord`` instances and the final log text.
119108
This is useful for when you want to assert on the contents of a message::
120109

121110
def test_baz(caplog):
@@ -146,12 +135,21 @@ You can call ``caplog.clear()`` to reset the captured log records in a test::
146135
your_test_method()
147136
assert ['Foo'] == [rec.message for rec in caplog.records]
148137

138+
139+
.. _live_logs:
140+
141+
142+
caplog fixture API
143+
^^^^^^^^^^^^^^^^^^
144+
145+
.. autoclass:: _pytest.logging.LogCaptureFixture
146+
:members:
147+
149148
Live Logs
150149
^^^^^^^^^
151150

152-
By default, pytest will output any logging records with a level higher or
153-
equal to WARNING. In order to actually see these logs in the console you have to
154-
disable pytest output capture by passing ``-s``.
151+
By setting the :confval:`log_cli` configuration option to ``true``, pytest will output
152+
logging records as they are emitted directly into the console.
155153

156154
You can specify the logging level for which log records with equal or higher
157155
level are printed to the console by passing ``--log-cli-level``. This setting
@@ -190,3 +188,50 @@ option names are:
190188
* ``log_file_level``
191189
* ``log_file_format``
192190
* ``log_file_date_format``
191+
192+
193+
.. _log_release_notes:
194+
195+
Release notes
196+
^^^^^^^^^^^^^
197+
198+
This feature was introduced as a drop-in replacement for the `pytest-catchlog
199+
<https://pypi.org/project/pytest-catchlog/>`_ plugin and they conflict
200+
with each other. The backward compatibility API with ``pytest-capturelog``
201+
has been dropped when this feature was introduced, so if for that reason you
202+
still need ``pytest-catchlog`` you can disable the internal feature by
203+
adding to your ``pytest.ini``:
204+
205+
.. code-block:: ini
206+
207+
[pytest]
208+
addopts=-p no:logging
209+
210+
211+
.. _log_changes_3_4:
212+
213+
Incompatible changes in pytest 3.4
214+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
215+
216+
This feature was introduced in ``3.3`` and some **incompatible changes** have been
217+
made in ``3.4`` after community feedback:
218+
219+
* Log levels are no longer changed unless explicitly requested by the :confval:`log_level` configuration
220+
or ``--log-level`` command-line options. This allows users to configure logger objects themselves.
221+
* :ref:`Live Logs <live_logs>` is now disabled by default and can be enabled setting the
222+
:confval:`log_cli` configuration option to ``true``.
223+
* :ref:`Live Logs <live_logs>` are now sent to ``sys.stdout`` and no longer require the ``-s`` command-line option
224+
to work.
225+
226+
If you want to partially restore the logging behavior of version ``3.3``, you can add this options to your ``ini``
227+
file:
228+
229+
.. code-block:: ini
230+
231+
[pytest]
232+
log_cli=true
233+
log_level=NOTSET
234+
235+
More details about the discussion that lead to this changes can be read in
236+
issue `#3013 <https://github.com/pytest-dev/pytest/issues/3013>`_.
237+

0 commit comments

Comments
 (0)