Python twisted.logger.ILogObserver() Examples

The following are 12 code examples of twisted.logger.ILogObserver(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted.logger , or try the search function .
Example #1
Source File: test_twistd.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_loggerComponentBeatsLegacyLoggerComponent(self):
        """
        A L{ILogObserver} takes precedence over a L{LegacyILogObserver}
        component set on Application.
        """
        nonlogs = []
        observer = self._makeObserver()
        application = Componentized()
        application.setComponent(ILogObserver, observer)
        application.setComponent(LegacyILogObserver, nonlogs.append)

        logger = app.AppLogger({})
        logger.start(application)

        self._checkObserver(observer)
        self.assertEqual(nonlogs, []) 
Example #2
Source File: test_twistd.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_loggerComponentBeatsLegacyLoggerComponent(self):
        """
        A L{ILogObserver} takes precedence over a L{LegacyILogObserver}
        component set on Application.
        """
        nonlogs = []
        observer = self._makeObserver()
        application = Componentized()
        application.setComponent(ILogObserver, observer)
        application.setComponent(LegacyILogObserver, nonlogs.append)

        logger = app.AppLogger({})
        logger.start(application)

        self._checkObserver(observer)
        self.assertEqual(nonlogs, []) 
Example #3
Source File: test_twistd.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_unmarkedObserversDeprecated(self):
        """
        L{app.AppLogger} using a logger observer which does not implement
        L{ILogObserver} or L{LegacyILogObserver} will be wrapped in a compat
        shim and raise a L{DeprecationWarning}.
        """
        logs = []
        logger = app.AppLogger({})
        logger._getLogObserver = lambda: logs.append
        logger.start(Componentized())

        self.assertIn("starting up", textFromEventDict(logs[0]))

        warnings = self.flushWarnings(
            [self.test_unmarkedObserversDeprecated])
        self.assertEqual(len(warnings), 1)
        self.assertEqual(warnings[0]["message"],
                         ("Passing a logger factory which makes log observers "
                          "which do not implement twisted.logger.ILogObserver "
                          "or twisted.python.log.ILogObserver to "
                          "twisted.application.app.AppLogger was deprecated "
                          "in Twisted 16.2. Please use a factory that "
                          "produces twisted.logger.ILogObserver (or the "
                          "legacy twisted.python.log.ILogObserver) "
                          "implementing objects instead.")) 
Example #4
Source File: test_twistd.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def _makeObserver(self):
        """
        Make a new observer which captures all logs sent to it.

        @return: An observer that stores all logs sent to it.
        @rtype: Callable that implements L{ILogObserver}.
        """
        @implementer(ILogObserver)
        class TestObserver(object):
            _logs = []

            def __call__(self, event):
                self._logs.append(event)

        return TestObserver() 
Example #5
Source File: test_twistd.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_startUsesApplicationLogObserver(self):
        """
        When the L{ILogObserver} component is available on the application,
        that object will be used as the log observer instead of constructing a
        new one.
        """
        application = Componentized()
        observer = self._makeObserver()
        application.setComponent(ILogObserver, observer)
        logger = app.AppLogger({})
        logger.start(application)
        self._checkObserver(observer) 
Example #6
Source File: test_twistd.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_configuredLogObserverBeatsComponent(self):
        """
        C{--logger} takes precedence over a L{ILogObserver} component set on
        Application.
        """
        observer = self._makeObserver()
        application = Componentized()
        application.setComponent(ILogObserver, observer)
        self._checkObserver(self._setupConfiguredLogger(application))
        self.assertEqual(observer._logs, []) 
Example #7
Source File: testing.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def __call__(self, event):
        """
        @see: L{ILogObserver}
        """
        self._events.append(event) 
Example #8
Source File: test_endpoints.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_resolveNameFailure(self):
        """
        A resolution failure is logged with the name that failed to
        resolve and the callable that tried to resolve it.  The
        resolution receiver begins, receives no addresses, and
        completes.
        """
        logs = []

        @provider(ILogObserver)
        def captureLogs(event):
            logs.append(event)

        globalLogPublisher.addObserver(captureLogs)
        self.addCleanup(lambda: globalLogPublisher.removeObserver(captureLogs))

        receiver = self.resolver.resolveHostName(self.receiver, "example.com")

        self.assertIs(receiver, self.receiver)

        self.fakeResolverReturns.errback(Exception())

        self.assertEqual(1, len(logs))
        self.assertEqual(1, len(self.flushLoggedErrors(Exception)))

        [event] = logs
        self.assertTrue(event.get("isError"))
        self.assertTrue(event.get("name", "example.com"))
        self.assertTrue(event.get("callable", repr(self.fakeResolver)))

        self.assertEqual(1, len(self.resolutionBeganCalls))
        self.assertEqual(self.resolutionBeganCalls[0].name, "example.com")
        self.assertFalse(self.addressResolvedCalls)
        self.assertEqual(1, self.resolutionCompleteCallCount) 
Example #9
Source File: test_twistd.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def _makeObserver(self):
        """
        Make a new observer which captures all logs sent to it.

        @return: An observer that stores all logs sent to it.
        @rtype: Callable that implements L{ILogObserver}.
        """
        @implementer(ILogObserver)
        class TestObserver(object):
            _logs = []

            def __call__(self, event):
                self._logs.append(event)

        return TestObserver() 
Example #10
Source File: test_twistd.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_startUsesApplicationLogObserver(self):
        """
        When the L{ILogObserver} component is available on the application,
        that object will be used as the log observer instead of constructing a
        new one.
        """
        application = Componentized()
        observer = self._makeObserver()
        application.setComponent(ILogObserver, observer)
        logger = app.AppLogger({})
        logger.start(application)
        self._checkObserver(observer) 
Example #11
Source File: app.py    From Safejumper-for-Desktop with GNU General Public License v2.0 4 votes vote down vote up
def start(self, application):
        """
        Initialize the global logging system for the given application.

        If a custom logger was specified on the command line it will be used.
        If not, and an L{logger.ILogObserver} or legacy L{log.ILogObserver}
        component has been set on C{application}, then it will be used as the
        log observer. Otherwise a log observer will be created based on the
        command line options for built-in loggers (e.g. C{--logfile}).

        @param application: The application on which to check for an
            L{logger.ILogObserver} or legacy L{log.ILogObserver}.
        @type application: L{twisted.python.components.Componentized}
        """
        if self._observerFactory is not None:
            observer = self._observerFactory()
        else:
            observer = application.getComponent(logger.ILogObserver, None)
            if observer is None:
                # If there's no new ILogObserver, try the legacy one
                observer = application.getComponent(log.ILogObserver, None)

        if observer is None:
            observer = self._getLogObserver()
        self._observer = observer

        if logger.ILogObserver.providedBy(self._observer):
            observers = [self._observer]
        elif log.ILogObserver.providedBy(self._observer):
            observers = [logger.LegacyLogObserverWrapper(self._observer)]
        else:
            warnings.warn(
                ("Passing a logger factory which makes log observers which do "
                 "not implement twisted.logger.ILogObserver or "
                 "twisted.python.log.ILogObserver to "
                 "twisted.application.app.AppLogger was deprecated in "
                 "Twisted 16.2. Please use a factory that produces "
                 "twisted.logger.ILogObserver (or the legacy "
                 "twisted.python.log.ILogObserver) implementing objects "
                 "instead."),
                DeprecationWarning,
                stacklevel=2)
            observers = [logger.LegacyLogObserverWrapper(self._observer)]

        logger.globalLogBeginner.beginLoggingTo(observers)
        self._initialLog() 
Example #12
Source File: app.py    From learn_python3_spider with MIT License 4 votes vote down vote up
def start(self, application):
        """
        Initialize the global logging system for the given application.

        If a custom logger was specified on the command line it will be used.
        If not, and an L{logger.ILogObserver} or legacy L{log.ILogObserver}
        component has been set on C{application}, then it will be used as the
        log observer. Otherwise a log observer will be created based on the
        command line options for built-in loggers (e.g. C{--logfile}).

        @param application: The application on which to check for an
            L{logger.ILogObserver} or legacy L{log.ILogObserver}.
        @type application: L{twisted.python.components.Componentized}
        """
        if self._observerFactory is not None:
            observer = self._observerFactory()
        else:
            observer = application.getComponent(logger.ILogObserver, None)
            if observer is None:
                # If there's no new ILogObserver, try the legacy one
                observer = application.getComponent(log.ILogObserver, None)

        if observer is None:
            observer = self._getLogObserver()
        self._observer = observer

        if logger.ILogObserver.providedBy(self._observer):
            observers = [self._observer]
        elif log.ILogObserver.providedBy(self._observer):
            observers = [logger.LegacyLogObserverWrapper(self._observer)]
        else:
            warnings.warn(
                ("Passing a logger factory which makes log observers which do "
                 "not implement twisted.logger.ILogObserver or "
                 "twisted.python.log.ILogObserver to "
                 "twisted.application.app.AppLogger was deprecated in "
                 "Twisted 16.2. Please use a factory that produces "
                 "twisted.logger.ILogObserver (or the legacy "
                 "twisted.python.log.ILogObserver) implementing objects "
                 "instead."),
                DeprecationWarning,
                stacklevel=2)
            observers = [logger.LegacyLogObserverWrapper(self._observer)]

        logger.globalLogBeginner.beginLoggingTo(observers)
        self._initialLog()