我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用twisted.python.log.ILogObserver()。
def __init__(self, *args): self.ip = args[0] self.port = args[1] application = service.Application("kademlia") application.setComponent(ILogObserver, log.FileLogObserver(sys.stdout, log.INFO).emit) if os.path.isfile('cache.pickle'): kserver = Server.loadState('cache.pickle') else: kserver = Server() kserver.bootstrap([(self.ip, self.port)]) kserver.saveStateRegularly('cache.pickle', 10) server = internet.UDPServer(self.port, kserver.protocol) server.setServiceParent(application) self.log = logging.getLogger(resource_filename(__name__, __file__))
def setupTwistedLogging(application): """Setup a L{LogFile} for the given application. @param application: A C{twisted.application.service.Application} instance. """ application.setComponent(ILogObserver, PythonLoggingObserver(None).emit)
def setServiceParent(self, parent): service.MultiService.setServiceParent(self, parent) if isinstance(parent, Componentized): parent.setComponent(ILogObserver, rurouniLogObserver)
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()