def _setup_cli_logging(self): """Sets up the handler and logger for the Live Logs feature, if enabled.""" terminal_reporter = self._config.pluginmanager.get_plugin( "terminalreporter") if self._log_cli_enabled() and terminal_reporter is not None: capture_manager = self._config.pluginmanager.get_plugin( "capturemanager") log_cli_handler = _LiveLoggingStreamHandler( terminal_reporter, capture_manager) log_cli_format = get_option_ini(self._config, "log_cli_format", "log_format") log_cli_date_format = get_option_ini(self._config, "log_cli_date_format", "log_date_format") if (self._config.option.color != "no" and ColoredLevelFormatter.LEVELNAME_FMT_REGEX.search( log_cli_format)): log_cli_formatter = ColoredLevelFormatter( create_terminal_writer(self._config), log_cli_format, datefmt=log_cli_date_format, ) else: log_cli_formatter = logging.Formatter( log_cli_format, datefmt=log_cli_date_format) log_cli_level = get_actual_log_level(self._config, "log_cli_level", "log_level") self.log_cli_handler = log_cli_handler self.live_logs_context = lambda: catching_logs(log_cli_handler, formatter= log_cli_formatter, level=log_cli_level) else: self.live_logs_context = lambda: dummy_context_manager()
def global_and_fixture_disabled(self): """Context manager to temporarily disables global and current fixture capturing.""" # Need to undo local capsys-et-al if exists before disabling global capture fixture = getattr(self._current_item, "_capture_fixture", None) ctx_manager = fixture._suspend() if fixture else dummy_context_manager() with ctx_manager: self.suspend_global_capture(item=None, in_=False) try: yield finally: self.resume_global_capture()
def __init__(self, config): """Creates a new plugin to capture log messages. The formatter can be safely shared across all handlers so create a single one for the entire test session here. """ self._config = config # enable verbose output automatically if live logging is enabled if self._log_cli_enabled() and not config.getoption("verbose"): config.option.verbose = 1 self.print_logs = get_option_ini(config, "log_print") self.formatter = logging.Formatter( get_option_ini(config, "log_format"), get_option_ini(config, "log_date_format"), ) self.log_level = get_actual_log_level(config, "log_level") log_file = get_option_ini(config, "log_file") if log_file: self.log_file_level = get_actual_log_level(config, "log_file_level") log_file_format = get_option_ini(config, "log_file_format", "log_format") log_file_date_format = get_option_ini( config, "log_file_date_format", "log_date_format" ) # Each pytest runtests session will write to a clean logfile self.log_file_handler = logging.FileHandler( log_file, mode="w", encoding="UTF-8" ) log_file_formatter = logging.Formatter( log_file_format, datefmt=log_file_date_format ) self.log_file_handler.setFormatter(log_file_formatter) else: self.log_file_handler = None self.log_cli_handler = None self.live_logs_context = lambda: dummy_context_manager() # Note that the lambda for the live_logs_context is needed because # live_logs_context can otherwise not be entered multiple times due # to limitations of contextlib.contextmanager. if self._log_cli_enabled(): self._setup_cli_logging()
def emit(self, record): ctx_manager = (self.capture_manager.global_and_fixture_disabled() if self.capture_manager else dummy_context_manager()) with ctx_manager: if not self._first_record_emitted: self.stream.write("\n") self._first_record_emitted = True elif self._when in ("teardown", "finish"): if not self._test_outcome_written: self._test_outcome_written = True self.stream.write("\n") if not self._section_name_shown and self._when: self.stream.section("live log " + self._when, sep="-", bold=True) self._section_name_shown = True logging.StreamHandler.emit(self, record)
def __init__(self, config): """Creates a new plugin to capture log messages. The formatter can be safely shared across all handlers so create a single one for the entire test session here. """ self._config = config # enable verbose output automatically if live logging is enabled if self._log_cli_enabled() and config.getoption("verbose") < 1: config.option.verbose = 1 self.print_logs = get_option_ini(config, "log_print") self.formatter = logging.Formatter( get_option_ini(config, "log_format"), get_option_ini(config, "log_date_format"), ) self.log_level = get_actual_log_level(config, "log_level") self.log_file_level = get_actual_log_level(config, "log_file_level") self.log_file_format = get_option_ini(config, "log_file_format", "log_format") self.log_file_date_format = get_option_ini( config, "log_file_date_format", "log_date_format" ) self.log_file_formatter = logging.Formatter( self.log_file_format, datefmt=self.log_file_date_format ) log_file = get_option_ini(config, "log_file") if log_file: self.log_file_handler = logging.FileHandler( log_file, mode="w", encoding="UTF-8" ) self.log_file_handler.setFormatter(self.log_file_formatter) else: self.log_file_handler = None self.log_cli_handler = None self.live_logs_context = lambda: dummy_context_manager() # Note that the lambda for the live_logs_context is needed because # live_logs_context can otherwise not be entered multiple times due # to limitations of contextlib.contextmanager. if self._log_cli_enabled(): self._setup_cli_logging()
def emit(self, record): ctx_manager = ( self.capture_manager.global_and_fixture_disabled() if self.capture_manager else dummy_context_manager() ) with ctx_manager: if not self._first_record_emitted: self.stream.write("\n") self._first_record_emitted = True elif self._when in ("teardown", "finish"): if not self._test_outcome_written: self._test_outcome_written = True self.stream.write("\n") if not self._section_name_shown and self._when: self.stream.section("live log " + self._when, sep="-", bold=True) self._section_name_shown = True logging.StreamHandler.emit(self, record)
def _setup_cli_logging(self): """Sets up the handler and logger for the Live Logs feature, if enabled. This must be done right before starting the loop so we can access the terminal reporter plugin. """ terminal_reporter = self._config.pluginmanager.get_plugin("terminalreporter") if self._log_cli_enabled() and terminal_reporter is not None: capture_manager = self._config.pluginmanager.get_plugin("capturemanager") log_cli_handler = _LiveLoggingStreamHandler( terminal_reporter, capture_manager ) log_cli_format = get_option_ini( self._config, "log_cli_format", "log_format" ) log_cli_date_format = get_option_ini( self._config, "log_cli_date_format", "log_date_format" ) if ( self._config.option.color != "no" and ColoredLevelFormatter.LEVELNAME_FMT_REGEX.search(log_cli_format) ): log_cli_formatter = ColoredLevelFormatter( create_terminal_writer(self._config), log_cli_format, datefmt=log_cli_date_format, ) else: log_cli_formatter = logging.Formatter( log_cli_format, datefmt=log_cli_date_format ) log_cli_level = get_actual_log_level( self._config, "log_cli_level", "log_level" ) self.log_cli_handler = log_cli_handler self.live_logs_context = catching_logs( log_cli_handler, formatter=log_cli_formatter, level=log_cli_level ) else: self.live_logs_context = dummy_context_manager()