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