Skip to content

Instantly share code, notes, and snippets.

@truemped
Created February 4, 2011 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save truemped/811062 to your computer and use it in GitHub Desktop.
Save truemped/811062 to your computer and use it in GitHub Desktop.
A sink for the pyzmq log handler
# Copyright (c) 2011 Daniel Truemper <truemped@googlemail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions: The above copyright notice and this
# permission notice shall be included in all copies or substantial portions of
# the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
# EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
# OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
"""
Module for aggregating spyder logs.
"""
import logging
import logging.config
import signal
import zmq
from zmq.eventloop.ioloop import IOLoop
from zmq.eventloop.zmqstream import ZMQStream
logging.config.fileConfig('logging.conf')
LOGGERS = { "default" : logging.getLogger() }
LOGGERS['master'] = logging.getLogger('masterlog')
LOGGERS['worker'] = logging.getLogger('workerlog')
def log_zmq_message(msg):
"""
Log a specific message.
The message has the format::
message = [topic, msg]
`topic` is a string of the form::
topic = "process.LEVEL.subtopics"
"""
t = msg[0].split(".")
if len(t) == 2:
t.append("SUBTOPIC")
if t[0] in LOGGERS:
l = getattr(LOGGERS[t[0]], t[1].lower())
l("%s - %s" % (t[2], msg[1].strip()))
else:
l = getattr(LOGGERS['default'], t[1].lower())
l("%s: %s)" % (t[2], msg[2].strip()))
def main(settings):
"""
Initialize the logger sink.
"""
ctx = zmq.Context()
io_loop = IOLoop.instance()
log_sub = ctx.socket(zmq.SUB)
log_sub.setsockopt(zmq.SUBSCRIBE, "")
log_sub.bind(settings.ZEROMQ_LOGGING)
log_stream = ZMQStream(log_sub, io_loop)
log_stream.on_recv(log_zmq_message)
def handle_shutdown_signal(sig, frame):
log_stream.stop_on_recv()
log_stream.flush()
io_loop.stop()
# handle kill signals
signal.signal(signal.SIGINT, handle_shutdown_signal)
signal.signal(signal.SIGTERM, handle_shutdown_signal)
io_loop.start()
log_stream.close()
ctx.term()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment