2
2
3
3
import logging
4
4
from contextlib import closing , contextmanager
5
- import sys
6
5
import six
7
6
8
7
import pytest
@@ -263,30 +262,13 @@ def __init__(self, config):
263
262
The formatter can be safely shared across all handlers so
264
263
create a single one for the entire test session here.
265
264
"""
265
+ self ._config = config
266
266
self .print_logs = get_option_ini (config , 'log_print' )
267
267
self .formatter = logging .Formatter (
268
268
get_option_ini (config , 'log_format' ),
269
269
get_option_ini (config , 'log_date_format' ))
270
270
self .log_level = get_actual_log_level (config , 'log_level' )
271
271
272
- if config .getini ('log_cli' ):
273
- log_cli_handler = logging .StreamHandler (sys .stderr )
274
- log_cli_format = get_option_ini (
275
- config , 'log_cli_format' , 'log_format' )
276
- log_cli_date_format = get_option_ini (
277
- config , 'log_cli_date_format' , 'log_date_format' )
278
- log_cli_formatter = logging .Formatter (
279
- log_cli_format ,
280
- datefmt = log_cli_date_format )
281
- log_cli_level = get_actual_log_level (config , 'log_cli_level' , 'log_level' )
282
- self .log_cli_handler = log_cli_handler # needed for a single unittest
283
- self .live_logs_context = catching_logs (log_cli_handler ,
284
- formatter = log_cli_formatter ,
285
- level = log_cli_level )
286
- else :
287
- self .log_cli_handler = None
288
- self .live_logs_context = _dummy_context_manager ()
289
-
290
272
log_file = get_option_ini (config , 'log_file' )
291
273
if log_file :
292
274
self .log_file_level = get_actual_log_level (config , 'log_file_level' )
@@ -340,6 +322,7 @@ def pytest_runtest_teardown(self, item):
340
322
@pytest .hookimpl (hookwrapper = True )
341
323
def pytest_runtestloop (self , session ):
342
324
"""Runs all collected test items."""
325
+ self ._setup_cli_logging ()
343
326
with self .live_logs_context :
344
327
if self .log_file_handler is not None :
345
328
with closing (self .log_file_handler ):
@@ -348,3 +331,27 @@ def pytest_runtestloop(self, session):
348
331
yield # run all the tests
349
332
else :
350
333
yield # run all the tests
334
+
335
+ def _setup_cli_logging (self ):
336
+ """Setups the handler and logger for the Live Logs feature, if enabled.
337
+
338
+ This must be done right before starting the loop so we can access the terminal reporter plugin.
339
+ """
340
+ terminal_reporter = self ._config .pluginmanager .get_plugin ('terminalreporter' )
341
+ if self ._config .getini ('log_cli' ) and terminal_reporter is not None :
342
+ log_cli_handler = logging .StreamHandler (terminal_reporter ._tw )
343
+ log_cli_format = get_option_ini (
344
+ self ._config , 'log_cli_format' , 'log_format' )
345
+ log_cli_date_format = get_option_ini (
346
+ self ._config , 'log_cli_date_format' , 'log_date_format' )
347
+ log_cli_formatter = logging .Formatter (
348
+ log_cli_format ,
349
+ datefmt = log_cli_date_format )
350
+ log_cli_level = get_actual_log_level (self ._config , 'log_cli_level' , 'log_level' )
351
+ self .log_cli_handler = log_cli_handler # needed for a single unittest
352
+ self .live_logs_context = catching_logs (log_cli_handler ,
353
+ formatter = log_cli_formatter ,
354
+ level = log_cli_level )
355
+ else :
356
+ self .log_cli_handler = None
357
+ self .live_logs_context = _dummy_context_manager ()
0 commit comments