@@ -76,6 +76,9 @@ def add_option_ini(option, dest, default=None, type=None, **kwargs):
76
76
'--log-file-date-format' ,
77
77
dest = 'log_file_date_format' , default = DEFAULT_LOG_DATE_FORMAT ,
78
78
help = 'log date format as used by the logging module.' )
79
+ add_option_ini (
80
+ '--capture-log' , choices = ['on-failure' , 'off' , 'live' ],
81
+ dest = 'capture_log' , default = 'on-failure' )
79
82
80
83
81
84
@contextmanager
@@ -117,6 +120,16 @@ def emit(self, record):
117
120
logging .StreamHandler .emit (self , record )
118
121
119
122
123
+ class TerminalWriterHandler (logging .Handler ):
124
+ def __init__ (self ):
125
+ # TODO sys.stderr is captured by default ?!??!
126
+ self .tw = py .io .TerminalWriter (sys .stderr )
127
+ super (TerminalWriterHandler , self ).__init__ ()
128
+
129
+ def emit (self , record ):
130
+ self .tw .write (self .format (record ))
131
+
132
+
120
133
class LogCaptureFixture (object ):
121
134
"""Provides access and control of log capturing."""
122
135
@@ -241,6 +254,8 @@ def __init__(self, config):
241
254
The formatter can be safely shared across all handlers so
242
255
create a single one for the entire test session here.
243
256
"""
257
+ self .capture_log = get_option_ini (config , 'capture_log' )
258
+
244
259
self .log_cli_level = get_actual_log_level (
245
260
config , 'log_cli_level' , 'log_level' ) or logging .WARNING
246
261
@@ -249,18 +264,14 @@ def __init__(self, config):
249
264
get_option_ini (config , 'log_format' ),
250
265
get_option_ini (config , 'log_date_format' ))
251
266
252
- log_cli_handler = logging . StreamHandler ( sys . stderr )
267
+
253
268
log_cli_format = get_option_ini (
254
269
config , 'log_cli_format' , 'log_format' )
255
270
log_cli_date_format = get_option_ini (
256
271
config , 'log_cli_date_format' , 'log_date_format' )
257
- log_cli_formatter = logging .Formatter (
272
+ self . log_cli_formatter = logging .Formatter (
258
273
log_cli_format ,
259
274
datefmt = log_cli_date_format )
260
- self .log_cli_handler = log_cli_handler # needed for a single unittest
261
- self .live_logs = catching_logs (log_cli_handler ,
262
- formatter = log_cli_formatter ,
263
- level = self .log_cli_level )
264
275
265
276
log_file = get_option_ini (config , 'log_file' )
266
277
if log_file :
@@ -316,11 +327,28 @@ def pytest_runtest_teardown(self, item):
316
327
@pytest .hookimpl (hookwrapper = True )
317
328
def pytest_runtestloop (self , session ):
318
329
"""Runs all collected test items."""
319
- with self .live_logs :
330
+
331
+ # TODO what should happen at the end of the tests?
332
+ if self .capture_log == 'live' :
333
+ with catching_logs (TerminalWriterHandler (),
334
+ formatter = self .log_cli_formatter ,
335
+ level = self .log_cli_level ):
336
+ if self .log_file_handler is not None :
337
+ with closing (self .log_file_handler ):
338
+ with catching_logs (self .log_file_handler ,
339
+ level = self .log_file_level ):
340
+ yield # run all the tests
341
+ else :
342
+ yield # run all the tests
343
+ elif self .capture_log == 'on-failure' :
320
344
if self .log_file_handler is not None :
321
345
with closing (self .log_file_handler ):
322
346
with catching_logs (self .log_file_handler ,
323
347
level = self .log_file_level ):
324
348
yield # run all the tests
325
349
else :
326
350
yield # run all the tests
351
+ elif self .capture_log == 'off' :
352
+ yield
353
+ else :
354
+ raise ValueError ('capture_log: %s' % capture_log )
0 commit comments