Пример #1
0
    def test_startLoggingOverridesWarning(self):
        """
        startLogging() overrides global C{warnings.showwarning} such that
        warnings go to Twisted log observers.
        """
        self._startLoggingCleanup()
        newPublisher = NewLogPublisher()

        class SysModule(object):
            stdout = object()
            stderr = object()

        tempLogPublisher = LogPublisher(
            newPublisher, newPublisher,
            logBeginner=LogBeginner(newPublisher, StringIO(), SysModule,
                                    warnings)
        )
        # Trial reports warnings in two ways.  First, it intercepts the global
        # 'showwarning' function *itself*, after starting logging (by way of
        # the '_collectWarnings' function which collects all warnings as a
        # around the test's 'run' method).  Second, it has a log observer which
        # immediately reports warnings when they're propagated into the log
        # system (which, in normal operation, happens only at the end of the
        # test case).  In order to avoid printing a spurious warning in this
        # test, we first replace the global log publisher's 'showwarning' in
        # the module with our own.
        self.patch(log, "theLogPublisher", tempLogPublisher)
        # And, one last thing, pretend we're starting from a fresh import, or
        # warnings.warn won't be patched at all.
        log._oldshowwarning = None
        # Global mutable state is bad, kids.  Stay in school.
        fakeFile = StringIO()
        # We didn't previously save log messages, so let's make sure we don't
        # save them any more.
        evt = {"pre-start": "event"}
        received = []

        def preStartObserver(x):
            if 'pre-start' in x.keys():
                received.append(x)

        newPublisher(evt)
        newPublisher.addObserver(preStartObserver)
        log.startLogging(fakeFile, setStdout=False)
        self.addCleanup(tempLogPublisher._stopLogging)
        self.assertEqual(received, [])
        warnings.warn("hello!")
        output = fakeFile.getvalue()
        self.assertIn("UserWarning: hello!", output)
Пример #2
0
 def __init__(self, observerPublisher=None, publishPublisher=None,
              logBeginner=None, warningsModule=warnings):
     if publishPublisher is None:
         publishPublisher = NewPublisher()
         if observerPublisher is None:
             observerPublisher = publishPublisher
     if observerPublisher is None:
         observerPublisher = NewPublisher()
     self._observerPublisher = observerPublisher
     self._publishPublisher = publishPublisher
     self._legacyObservers = []
     if logBeginner is None:
         # This default behavior is really only used for testing.
         beginnerPublisher = NewPublisher()
         beginnerPublisher.addObserver(observerPublisher)
         logBeginner = LogBeginner(beginnerPublisher, NullFile(), sys,
                                   warnings)
     self._logBeginner = logBeginner
     self._warningsModule = warningsModule
     self._oldshowwarning = warningsModule.showwarning
     self.showwarning = self._logBeginner.showwarning
Пример #3
0
    def test_old_style(self):
        """
        L{Logger} handles old style log strings.
        """
        observer = LogPublisher()

        observed = []
        observer.addObserver(observed.append)

        sio = StringIO()
        observer.addObserver(textFileLogObserver(sio))

        logger = Logger(observer=observer)

        index = 0
        logger.info("test")
        self.assertEqual(observed[index]["log_level"], LogLevel.info)
        self.assertEqual(observed[index]["log_format"], u"{msg}")
        self.assertEqual(observed[index]["msg"], u"test")
        self.assertEqual(
            sio.getvalue().splitlines()[index].split("#info] ")[1], "test")

        index += 1
        logger.info("test {}")
        self.assertEqual(observed[index]["log_level"], LogLevel.info)
        self.assertEqual(observed[index]["log_format"], u"{msg}")
        self.assertEqual(observed[index]["msg"], u"test {}")
        self.assertEqual(
            sio.getvalue().splitlines()[index].split("#info] ")[1], "test {}")

        index += 1
        logger.info("test {foo}")
        self.assertEqual(observed[index]["log_level"], LogLevel.info)
        self.assertEqual(observed[index]["log_format"], u"{msg}")
        self.assertEqual(observed[index]["msg"], u"test {foo}")
        self.assertEqual(
            sio.getvalue().splitlines()[index].split("#info] ")[1],
            "test {foo}")
Пример #4
0
 def __init__(self,
              observerPublisher=None,
              publishPublisher=None,
              logBeginner=None,
              warningsModule=warnings):
     if publishPublisher is None:
         publishPublisher = NewPublisher()
         if observerPublisher is None:
             observerPublisher = publishPublisher
     if observerPublisher is None:
         observerPublisher = NewPublisher()
     self._observerPublisher = observerPublisher
     self._publishPublisher = publishPublisher
     self._legacyObservers = []
     if logBeginner is None:
         # This default behavior is really only used for testing.
         beginnerPublisher = NewPublisher()
         beginnerPublisher.addObserver(observerPublisher)
         logBeginner = LogBeginner(beginnerPublisher, NullFile(), sys,
                                   warnings)
     self._logBeginner = logBeginner
     self._warningsModule = warningsModule
     self._oldshowwarning = warningsModule.showwarning
     self.showwarning = self._logBeginner.showwarning