1818
1919DEFAULT_LOG_FORMAT = "%(levelname)-8s %(name)s:%(filename)s:%(lineno)d %(message)s"
2020DEFAULT_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 )
2126
2227
2328class ColoredLevelFormatter (logging .Formatter ):
@@ -257,8 +262,8 @@ def get_records(self, when):
257262
258263 @property
259264 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 () )
262267
263268 @property
264269 def records (self ):
@@ -394,7 +399,7 @@ def __init__(self, config):
394399 config .option .verbose = 1
395400
396401 self .print_logs = get_option_ini (config , "log_print" )
397- self .formatter = logging . Formatter (
402+ self .formatter = self . _create_formatter (
398403 get_option_ini (config , "log_format" ),
399404 get_option_ini (config , "log_date_format" ),
400405 )
@@ -428,6 +433,19 @@ def __init__(self, config):
428433 if self ._log_cli_enabled ():
429434 self ._setup_cli_logging ()
430435
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+
431449 def _setup_cli_logging (self ):
432450 config = self ._config
433451 terminal_reporter = config .pluginmanager .get_plugin ("terminalreporter" )
@@ -438,23 +456,12 @@ def _setup_cli_logging(self):
438456 capture_manager = config .pluginmanager .get_plugin ("capturemanager" )
439457 # if capturemanager plugin is disabled, live logging still works.
440458 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" ),
444463 )
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+
458465 log_cli_level = get_actual_log_level (config , "log_cli_level" , "log_level" )
459466 self .log_cli_handler = log_cli_handler
460467 self .live_logs_context = lambda : catching_logs (
0 commit comments