MCPcopy Create free account
hub / github.com/EasyIME/PIME / ExpectLog

Class ExpectLog

python/python3/tornado/testing.py:700–785  ·  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

698
699
700class ExpectLog(logging.Filter):
701 """Context manager to capture and suppress expected log output.
702
703 Useful to make tests of error conditions less noisy, while still
704 leaving unexpected log entries visible. *Not thread safe.*
705
706 The attribute ``logged_stack`` is set to ``True`` if any exception
707 stack trace was logged.
708
709 Usage::
710
711 with ExpectLog('tornado.application', "Uncaught exception"):
712 error_response = self.fetch("/some_page")
713
714 .. versionchanged:: 4.3
715 Added the ``logged_stack`` attribute.
716 """
717
718 def __init__(
719 self,
720 logger: Union[logging.Logger, basestring_type],
721 regex: str,
722 required: bool = True,
723 level: Optional[int] = None,
724 ) -> None:
725 """Constructs an ExpectLog context manager.
726
727 :param logger: Logger object (or name of logger) to watch. Pass
728 an empty string to watch the root logger.
729 :param regex: Regular expression to match. Any log entries on
730 the specified logger that match this regex will be suppressed.
731 :param required: If true, an exception will be raised if the end of
732 the ``with`` statement is reached without matching any log entries.
733 :param level: A constant from the ``logging`` module indicating the
734 expected log level. If this parameter is provided, only log messages
735 at this level will be considered to match. Additionally, the
736 supplied ``logger`` will have its level adjusted if necessary
737 (for the duration of the ``ExpectLog`` to enable the expected
738 message.
739
740 .. versionchanged:: 6.1
741 Added the ``level`` parameter.
742 """
743 if isinstance(logger, basestring_type):
744 logger = logging.getLogger(logger)
745 self.logger = logger
746 self.regex = re.compile(regex)
747 self.required = required
748 self.matched = False
749 self.logged_stack = False
750 self.level = level
751 self.orig_level = None # type: Optional[int]
752
753 def filter(self, record: logging.LogRecord) -> bool:
754 if record.exc_info:
755 self.logged_stack = True
756 message = record.getMessage()
757 if self.regex.match(message):

Calls

no outgoing calls