本文整理汇总了Python中twisted.logger.globalLogBeginner.beginLoggingTo函数的典型用法代码示例。如果您正苦于以下问题:Python beginLoggingTo函数的具体用法?Python beginLoggingTo怎么用?Python beginLoggingTo使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了beginLoggingTo函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: startLogging
def startLogging(settings, stream=None, level=LogLevel.debug):
global predicate
fileObserver = logObserver(stream)
predicate = LogLevelFilterPredicate(defaultLogLevel=level)
if settings.options.debug_mqtt:
predicate.setLogLevelForNamespace('kotori.daq.services.mig', LogLevel.debug)
predicate.setLogLevelForNamespace('kotori.daq.application.mqttkit', LogLevel.debug)
if settings.options.debug_mqtt_driver:
predicate.setLogLevelForNamespace('kotori.daq.intercom.mqtt', LogLevel.debug)
predicate.setLogLevelForNamespace('mqtt', LogLevel.debug)
predicate.setLogLevelForNamespace('paho.mqtt', LogLevel.debug)
else:
predicate.setLogLevelForNamespace('kotori.daq.intercom.mqtt', LogLevel.info)
predicate.setLogLevelForNamespace('mqtt', LogLevel.info)
predicate.setLogLevelForNamespace('paho.mqtt', LogLevel.info)
if settings.options.debug_influx:
predicate.setLogLevelForNamespace('kotori.daq.storage.influx', LogLevel.debug)
if settings.options.debug_io:
predicate.setLogLevelForNamespace('kotori.io', LogLevel.debug)
observers = [ FilteringLogObserver(observer=fileObserver, predicates=[predicate]) ]
globalLogBeginner.beginLoggingTo(observers)
示例2: run
def run(self):
self.factory = HTTPFactory(
self.channel_layer,
self.action_logger,
timeout=self.http_timeout,
websocket_timeout=self.websocket_timeout,
ping_interval=self.ping_interval,
ws_protocols=self.ws_protocols,
root_path=self.root_path,
)
# Redirect the Twisted log to nowhere
globalLogBeginner.beginLoggingTo([lambda _: None], redirectStandardIO=False, discardBuffer=True)
# Listen on a socket
if self.unix_socket:
reactor.listenUNIX(self.unix_socket, self.factory)
elif self.file_descriptor:
# socket returns the same socket if supplied with a fileno
sock = socket.socket(fileno=self.file_descriptor)
reactor.adoptStreamPort(self.file_descriptor, sock.family, self.factory)
else:
reactor.listenTCP(self.port, self.factory, interface=self.host)
if "twisted" in self.channel_layer.extensions:
logging.info("Using native Twisted mode on channel layer")
reactor.callLater(0, self.backend_reader_twisted)
else:
logging.info("Using busy-loop synchronous mode on channel layer")
reactor.callLater(0, self.backend_reader_sync)
reactor.callLater(2, self.timeout_checker)
reactor.run(installSignalHandlers=self.signal_handlers)
示例3: main
def main(reactor, *argv):
import argparse
a = argparse.ArgumentParser()
a.add_argument('number', type=int)
a.add_argument('subprocess', nargs='+')
args = a.parse_args(argv)
globalLogBeginner.beginLoggingTo([jsonFileLogObserver(sys.stdout)])
executablePath = distutils.spawn.find_executable(args.subprocess[0])
args.subprocess[0] = executablePath
collection = ProcessCollection()
reactor.addSystemEventTrigger("before", "shutdown", collection.stop)
processes = [RespawningProcess(reactor,
executablePath, args.subprocess,
usePTY=True)
for _ in xrange(args.number)]
collection.addProcesses(processes)
collection.start()
terminationDeferred = defer.Deferred()
stdio.StandardIO(FireOnInput(terminationDeferred))
return terminationDeferred
示例4: start_logging
def start_logging(out=None, level='info'):
"""
Start logging to the file-like object in ``out``. By default, this
is stdout.
"""
global _loggers, _observer, _log_level
if level not in log_levels:
raise RuntimeError(
"Invalid log level '{0}'; valid are: {1}".format(
level, ', '.join(log_levels)
)
)
if _loggers is None:
return
if out is None:
out = _stdout
if _loggers is not None:
for ref in _loggers:
instance = ref()
if instance:
instance._set_log_level(level)
_loggers = None
_log_level = level
_observer = _LogObserver(out)
if _NEW_LOGGER:
globalLogBeginner.beginLoggingTo([_observer])
else:
from twisted.python import log
log.startLogging(out)
示例5: start_logging
def start_logging(out=_stdout, level='info'):
"""
Start logging to the file-like object in ``out``. By default, this
is stdout.
"""
global _loggers, _observer, _log_level, _started_logging
if level not in log_levels:
raise RuntimeError(
"Invalid log level '{0}'; valid are: {1}".format(
level, ', '.join(log_levels)
)
)
if _started_logging:
return
_started_logging = True
_log_level = level
set_global_log_level(_log_level)
if out:
_observer = _LogObserver(out)
if _NEW_LOGGER:
_observers = []
if _observer:
_observers.append(_observer)
globalLogBeginner.beginLoggingTo(_observers)
else:
assert out, "out needs to be given a value if using Twisteds before 15.2"
from twisted.python import log
log.startLogging(out)
示例6: start_logging
def start_logging(level=LogLevel.info):
observers = []
predicate = LogLevelFilterPredicate(defaultLogLevel=level)
observers.append(FilteringLogObserver(observer=textFileLogObserver(sys.stdout), predicates=[predicate]))
globalLogBeginner.beginLoggingTo(observers)
示例7: run
def run(self):
self.factory = HTTPFactory(
self.channel_layer,
self.action_logger,
timeout=self.http_timeout,
websocket_timeout=self.websocket_timeout,
ping_interval=self.ping_interval,
ping_timeout=self.ping_timeout,
ws_protocols=self.ws_protocols,
root_path=self.root_path,
proxy_forwarded_address_header=self.proxy_forwarded_address_header,
proxy_forwarded_port_header=self.proxy_forwarded_port_header
)
if self.verbosity <= 1:
# Redirect the Twisted log to nowhere
globalLogBeginner.beginLoggingTo([lambda _: None], redirectStandardIO=False, discardBuffer=True)
else:
globalLogBeginner.beginLoggingTo([STDLibLogObserver(__name__)])
# Disabled deliberately for the moment as it's worse performing
if "twisted" in self.channel_layer.extensions and False:
logger.info("Using native Twisted mode on channel layer")
reactor.callLater(0, self.backend_reader_twisted)
else:
logger.info("Using busy-loop synchronous mode on channel layer")
reactor.callLater(0, self.backend_reader_sync)
reactor.callLater(2, self.timeout_checker)
for socket_description in self.endpoints:
logger.info("Listening on endpoint %s" % socket_description)
# Twisted requires str on python2 (not unicode) and str on python3 (not bytes)
ep = serverFromString(reactor, str(socket_description))
ep.listen(self.factory)
reactor.run(installSignalHandlers=self.signal_handlers)
示例8: main
def main(reactor, *argv):
argument_parser = argparse.ArgumentParser()
argument_parser.add_argument('solr_url')
argument_parser.add_argument('json_docs')
args = argument_parser.parse_args(argv)
globalLogBeginner.beginLoggingTo([jsonFileLogObserver(sys.stdout)])
return replay(args.json_docs, args.solr_url)
示例9: create_server
def create_server(
host="localhost", port=5000, debug=False,
observer_name="twisted", flask_app=None
):
"""
Create and setup twisted server
(only need to do a reactor.run() after)
:param host: Host address to bind to (default: localhost)
:type host: str
:param port: Port to bind to (default: 5000)
:type port: int
:param debug: Should use debug mode (default: False)
:type debug: bool
:param observer_name: Name of twisted observer to log to stdlib
(default: twisted)
if None -> do not create observer
:type observer_name: None | str
:param flask_app: Flask object to be served (default: None)
if None -> use imported app
:type flask_app: flask.Flask
:rtype: None
"""
if observer_name is not None:
observer = STDLibLogObserver(name=observer_name)
globalLogBeginner.beginLoggingTo([observer])
if flask_app is None:
flask_app = create_app(debug=debug)
# Create a Twisted Web resource for our WebSocket server
ws_factory = WebSocketServerFactory(
u"ws://{}:{}".format(host, port)
)
ws_factory.protocol = PluginServerProtocol
# Needed if Hixie76 is to be supported
# ws_factory.setProtocolOptions(allowHixie76=True)
ws_resource = WebSocketResource(ws_factory)
# Create a Twisted Web WSGI resource for our Flask server
wsgi_resource = WSGIResource(reactor, reactor.getThreadPool(), flask_app)
# Create a root resource serving everything via WSGI/Flask, but
# The path "/ws" served by our webocket
root_resource = WSGIRootResource(wsgi_resource, {'ws': ws_resource})
# Create a Twisted Web Site and run everything
site = Site(root_resource)
# Needed if Hixie76 is to be supported
# site.protocol = HTTPChannelHixie76Aware
reactor.listenTCP(port, site)
示例10: startLogging
def startLogging(console=True, filepath=None):
'''
Starts the global Twisted logger subsystem with maybe
stdout and/or a file specified in the config file
'''
global logLevelFilterPredicate
observers = []
if console:
observers.append( FilteringLogObserver(observer=textFileLogObserver(sys.stdout),
predicates=[logLevelFilterPredicate] ))
if filepath is not None and filepath != "":
observers.append( FilteringLogObserver(observer=textFileLogObserver(open(filepath,'a')),
predicates=[logLevelFilterPredicate] ))
globalLogBeginner.beginLoggingTo(observers)
示例11: go
def go(self, reactor):
data = Data("Hello world\n", "text/plain")
data.putChild("", data)
factory = Site(data)
# TODO: adoptStreamConnection should really support AF_UNIX
protocol = ConnectionFromManager(reactor, factory)
skt = fromfd(MAGIC_FILE_DESCRIPTOR, AF_UNIX, SOCK_STREAM)
os.close(MAGIC_FILE_DESCRIPTOR)
serverTransport = UNIXServer(skt, protocol, None, None, 1234, reactor)
protocol.makeConnection(serverTransport)
serverTransport.startReading()
globalLogBeginner.beginLoggingTo([protocol.sendLog])
factory.doStart()
return Deferred()
示例12: _reactor_start
def _reactor_start(self):
"""
Start the reactor if it is not already running
If someone else started it -> someone else should shut it down
"""
try:
if reactor.running:
observer = STDLibLogObserver(name='twisted')
globalLogBeginner.beginLoggingTo([observer])
reactor.run(False)
else:
self.info("Reactor already running")
self._reactor_shutdown = False
except ReactorAlreadyRunning:
self.info("Reactor already running")
self._reactor_shutdown = False
except:
self.exception("Failed to start reactor")
示例13: startLogging
def startLogging(self):
"""
Start the L{twisted.logger} logging system.
"""
logFile = self.logFile
fileLogObserverFactory = self.fileLogObserverFactory
fileLogObserver = fileLogObserverFactory(logFile)
logLevelPredicate = LogLevelFilterPredicate(
defaultLogLevel=self.defaultLogLevel
)
filteringObserver = FilteringLogObserver(
fileLogObserver, [logLevelPredicate]
)
globalLogBeginner.beginLoggingTo([filteringObserver])
示例14: run
def run():
from twisted.internet import reactor
root = logging.getLogger()
logging.getLogger('django').setLevel(logging.INFO)
logging.raiseExceptions = settings.DEBUG
logging._srcfile = None # Disable expensive collection of location information.
root.setLevel(logging.DEBUG if settings.DEBUG else logging.INFO)
root.addHandler(TwistedLoggerLogHandler())
observer = FilteringLogObserver(
FileLogObserver(sys.stdout, formatForSystemd),
[dropUnhandledHTTP2Shutdown],
)
globalLogBeginner.beginLoggingTo([observer], redirectStandardIO=False)
log.info("Yarrharr {version} starting", version=__version__)
factory = Site(Root(reactor, reactor.getThreadPool()), logPath=None)
endpoint = serverFromString(reactor, settings.SERVER_ENDPOINT)
reactor.addSystemEventTrigger('before', 'startup', endpoint.listen, factory)
updateLoop = AdaptiveLoopingCall(reactor, lambda: updateFeeds(reactor))
loopEndD = updateLoop.start()
loopEndD.addErrback(lambda f: log.failure("Polling loop broke", f))
@receiver(schedule_changed)
def threadPollNow(sender, **kwargs):
"""
When the `schedule_changed` signal is sent poke the polling loop. If it
is sleeping this will cause it to poll immediately. Otherwise this will
cause it to run the poll function immediately once it returns (running
it again protects against races).
"""
log.debug("Immediate poll triggered by {sender}", sender=sender)
reactor.callFromThread(updateLoop.poke)
def stopUpdateLoop():
updateLoop.stop()
return loopEndD
reactor.addSystemEventTrigger('before', 'shutdown', stopUpdateLoop)
reactor.run()
示例15: init
def init(debug=False):
debug_enabled = debug or os.environ.get('DEBUG', False)
logging_level = logging.DEBUG if debug_enabled else logging.INFO
logging.basicConfig(level=logging_level,
format='%(asctime)s [%(name)s] %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filemode='a')
logging.getLogger('gnupg').setLevel(logging.WARN)
logging.getLogger('gnupg').addFilter(PrivateKeyFilter())
def formatter(event):
event['log_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(event['log_time']))
event['log_level'] = event['log_level'].name.upper()
logstring = u'{log_time} [{log_namespace}] {log_level} ' + event['log_format'] + '\n'
return logstring.format(**event)
observers = [FileLogObserver(sys.stdout, formatter)]
globalLogBeginner.beginLoggingTo(observers)