MCPcopy
hub / github.com/tornadoweb/tornado / ExpectLog

Class ExpectLog

tornado/testing.py:642–752  ·  view source on GitHub ↗

Context manager to capture and suppress expected log output. Useful to make tests of error conditions less noisy, while still leaving unexpected log entries visible. *Not thread safe.* The attribute ``logged_stack`` is set to ``True`` if any exception stack trace was logged.

Source from the content-addressed store, hash-verified

640
641
642class ExpectLog(logging.Filter):
643 """Context manager to capture and suppress expected log output.
644
645 Useful to make tests of error conditions less noisy, while still
646 leaving unexpected log entries visible. *Not thread safe.*
647
648 The attribute ``logged_stack`` is set to ``True`` if any exception
649 stack trace was logged.
650
651 Usage::
652
653 with ExpectLog('tornado.application', "Uncaught exception"):
654 error_response = self.fetch("/some_page")
655
656 .. versionchanged:: 4.3
657 Added the ``logged_stack`` attribute.
658 """
659
660 def __init__(
661 self,
662 logger: Union[logging.Logger, basestring_type],
663 regex: str,
664 required: bool = True,
665 level: Optional[int] = None,
666 ) -> None:
667 """Constructs an ExpectLog context manager.
668
669 :param logger: Logger object (or name of logger) to watch. Pass an
670 empty string to watch the root logger.
671 :param regex: Regular expression to match. Any log entries on the
672 specified logger that match this regex will be suppressed.
673 :param required: If true, an exception will be raised if the end of the
674 ``with`` statement is reached without matching any log entries.
675 :param level: A constant from the ``logging`` module indicating the
676 expected log level. If this parameter is provided, only log messages
677 at this level will be considered to match. Additionally, the
678 supplied ``logger`` will have its level adjusted if necessary (for
679 the duration of the ``ExpectLog`` to enable the expected message.
680
681 .. versionchanged:: 6.1
682 Added the ``level`` parameter.
683
684 .. deprecated:: 6.3
685 In Tornado 7.0, only ``WARNING`` and higher logging levels will be
686 matched by default. To match ``INFO`` and lower levels, the ``level``
687 argument must be used. This is changing to minimize differences
688 between ``tornado.testing.main`` (which enables ``INFO`` logs by
689 default) and most other test runners (including those in IDEs)
690 which have ``INFO`` logs disabled by default.
691 """
692 if isinstance(logger, basestring_type):
693 logger = logging.getLogger(logger)
694 self.logger = logger
695 self.regex = re.compile(regex)
696 self.required = required
697 # matched and deprecated_level_matched are a counter for the respective event.
698 self.matched = 0
699 self.deprecated_level_matched = 0

Calls

no outgoing calls