17
17
18
18
DEFAULT_LOG_FORMAT = "%(levelname)-8s %(name)s:%(filename)s:%(lineno)d %(message)s"
19
19
DEFAULT_LOG_DATE_FORMAT = "%H:%M:%S"
20
+ _ANSI_ESCAPE_SEQ = re .compile (r"\x1b\[[\d;]+m" )
21
+
22
+
23
+ def _remove_ansi_escape_sequences (text ):
24
+ return _ANSI_ESCAPE_SEQ .sub ("" , text )
20
25
21
26
22
27
class ColoredLevelFormatter (logging .Formatter ):
@@ -256,8 +261,8 @@ def get_records(self, when):
256
261
257
262
@property
258
263
def text (self ):
259
- """Returns the log text."""
260
- return self .handler .stream .getvalue ()
264
+ """Returns the formatted log text."""
265
+ return _remove_ansi_escape_sequences ( self .handler .stream .getvalue () )
261
266
262
267
@property
263
268
def records (self ):
@@ -393,7 +398,7 @@ def __init__(self, config):
393
398
config .option .verbose = 1
394
399
395
400
self .print_logs = get_option_ini (config , "log_print" )
396
- self .formatter = logging . Formatter (
401
+ self .formatter = self . _create_formatter (
397
402
get_option_ini (config , "log_format" ),
398
403
get_option_ini (config , "log_date_format" ),
399
404
)
@@ -427,6 +432,19 @@ def __init__(self, config):
427
432
if self ._log_cli_enabled ():
428
433
self ._setup_cli_logging ()
429
434
435
+ def _create_formatter (self , log_format , log_date_format ):
436
+ # color option doesn't exist if terminal plugin is disabled
437
+ color = getattr (self ._config .option , "color" , "no" )
438
+ if color != "no" and ColoredLevelFormatter .LEVELNAME_FMT_REGEX .search (
439
+ log_format
440
+ ):
441
+ formatter = ColoredLevelFormatter (
442
+ create_terminal_writer (self ._config ), log_format , log_date_format
443
+ )
444
+ else :
445
+ formatter = logging .Formatter (log_format , log_date_format )
446
+ return formatter
447
+
430
448
def _setup_cli_logging (self ):
431
449
config = self ._config
432
450
terminal_reporter = config .pluginmanager .get_plugin ("terminalreporter" )
@@ -437,23 +455,12 @@ def _setup_cli_logging(self):
437
455
capture_manager = config .pluginmanager .get_plugin ("capturemanager" )
438
456
# if capturemanager plugin is disabled, live logging still works.
439
457
log_cli_handler = _LiveLoggingStreamHandler (terminal_reporter , capture_manager )
440
- log_cli_format = get_option_ini (config , "log_cli_format" , "log_format" )
441
- log_cli_date_format = get_option_ini (
442
- config , "log_cli_date_format" , "log_date_format"
458
+
459
+ log_cli_formatter = self ._create_formatter (
460
+ get_option_ini (config , "log_cli_format" , "log_format" ),
461
+ get_option_ini (config , "log_cli_date_format" , "log_date_format" ),
443
462
)
444
- if (
445
- config .option .color != "no"
446
- and ColoredLevelFormatter .LEVELNAME_FMT_REGEX .search (log_cli_format )
447
- ):
448
- log_cli_formatter = ColoredLevelFormatter (
449
- create_terminal_writer (config ),
450
- log_cli_format ,
451
- datefmt = log_cli_date_format ,
452
- )
453
- else :
454
- log_cli_formatter = logging .Formatter (
455
- log_cli_format , datefmt = log_cli_date_format
456
- )
463
+
457
464
log_cli_level = get_actual_log_level (config , "log_cli_level" , "log_level" )
458
465
self .log_cli_handler = log_cli_handler
459
466
self .live_logs_context = lambda : catching_logs (
0 commit comments