MCPcopy Create free account
hub / github.com/pytest-dev/pytest / LoggingPlugin

Class LoggingPlugin

src/_pytest/logging.py:528–750  ·  view source on GitHub ↗

Attaches to the logging module and captures log messages for each test.

Source from the content-addressed store, hash-verified

526
527
528class LoggingPlugin:
529 """Attaches to the logging module and captures log messages for each test."""
530
531 def __init__(self, config: Config) -> None:
532 """Create a new plugin to capture log messages.
533
534 The formatter can be safely shared across all handlers so
535 create a single one for the entire test session here.
536 """
537 self._config = config
538
539 # Report logging.
540 self.formatter = self._create_formatter(
541 get_option_ini(config, "log_format"),
542 get_option_ini(config, "log_date_format"),
543 get_option_ini(config, "log_auto_indent"),
544 )
545 self.log_level = get_log_level_for_setting(config, "log_level")
546 self.caplog_handler = LogCaptureHandler()
547 self.caplog_handler.setFormatter(self.formatter)
548 self.report_handler = LogCaptureHandler()
549 self.report_handler.setFormatter(self.formatter)
550
551 # File logging.
552 self.log_file_level = get_log_level_for_setting(config, "log_file_level")
553 log_file = get_option_ini(config, "log_file") or os.devnull
554 if log_file != os.devnull:
555 directory = os.path.dirname(os.path.abspath(log_file))
556 if not os.path.isdir(directory):
557 os.makedirs(directory)
558
559 self.log_file_handler = _FileHandler(log_file, mode="w", encoding="UTF-8")
560 log_file_format = get_option_ini(config, "log_file_format", "log_format")
561 log_file_date_format = get_option_ini(
562 config, "log_file_date_format", "log_date_format"
563 )
564
565 log_file_formatter = logging.Formatter(
566 log_file_format, datefmt=log_file_date_format
567 )
568 self.log_file_handler.setFormatter(log_file_formatter)
569
570 # CLI/live logging.
571 self.log_cli_level = get_log_level_for_setting(
572 config, "log_cli_level", "log_level"
573 )
574 if self._log_cli_enabled():
575 terminal_reporter = config.pluginmanager.get_plugin("terminalreporter")
576 capture_manager = config.pluginmanager.get_plugin("capturemanager")
577 # if capturemanager plugin is disabled, live logging still works.
578 self.log_cli_handler: Union[
579 _LiveLoggingStreamHandler, _LiveLoggingNullHandler
580 ] = _LiveLoggingStreamHandler(terminal_reporter, capture_manager)
581 else:
582 self.log_cli_handler = _LiveLoggingNullHandler()
583 log_cli_formatter = self._create_formatter(
584 get_option_ini(config, "log_cli_format", "log_format"),
585 get_option_ini(config, "log_cli_date_format", "log_date_format"),

Callers 1

pytest_configureFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected