Advertisement
Guest User

Untitled

a guest
Apr 5th, 2012
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. # Copyright (c) 2011 Daniel Truemper <truemped@googlemail.com>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy of
  4. # this software and associated documentation files (the "Software"), to deal in
  5. # the Software without restriction, including without limitation the rights to
  6. # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  7. # of the Software, and to permit persons to whom the Software is furnished to do
  8. # so, subject to the following conditions: The above copyright notice and this
  9. # permission notice shall be included in all copies or substantial portions of
  10. # the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
  13. # EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  14. # OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  15. # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  16. # DEALINGS IN THE SOFTWARE.
  17.  
  18. """
  19. Module for aggregating spyder logs.
  20. """
  21. import logging
  22. import logging.config
  23. import signal
  24.  
  25. import zmq
  26. from zmq.eventloop.ioloop import IOLoop
  27. from zmq.eventloop.zmqstream import ZMQStream
  28.  
  29.  
  30. logging.config.fileConfig('logging.conf')
  31. LOGGERS = { "default" : logging.getLogger() }
  32.  
  33. LOGGERS['master'] = logging.getLogger('masterlog')
  34. LOGGERS['worker'] = logging.getLogger('workerlog')
  35.  
  36.  
  37. def log_zmq_message(msg):
  38. """
  39. Log a specific message.
  40.  
  41. The message has the format::
  42.  
  43. message = [topic, msg]
  44.  
  45. `topic` is a string of the form::
  46.  
  47. topic = "process.LEVEL.subtopics"
  48. """
  49. t = msg[0].split(".")
  50. if len(t) == 2:
  51. t.append("SUBTOPIC")
  52. if t[0] in LOGGERS:
  53. l = getattr(LOGGERS[t[0]], t[1].lower())
  54. l("%s - %s" % (t[2], msg[1].strip()))
  55. else:
  56. l = getattr(LOGGERS['default'], t[1].lower())
  57. l("%s: %s)" % (t[2], msg[2].strip()))
  58.  
  59.  
  60. def main(settings):
  61. """
  62. Initialize the logger sink.
  63. """
  64.  
  65. ctx = zmq.Context()
  66. io_loop = IOLoop.instance()
  67.  
  68. log_sub = ctx.socket(zmq.SUB)
  69. log_sub.setsockopt(zmq.SUBSCRIBE, "")
  70. log_sub.bind(settings.ZEROMQ_LOGGING)
  71.  
  72. log_stream = ZMQStream(log_sub, io_loop)
  73.  
  74. log_stream.on_recv(log_zmq_message)
  75.  
  76. def handle_shutdown_signal(sig, frame):
  77. log_stream.stop_on_recv()
  78. log_stream.flush()
  79. io_loop.stop()
  80.  
  81. # handle kill signals
  82. signal.signal(signal.SIGINT, handle_shutdown_signal)
  83. signal.signal(signal.SIGTERM, handle_shutdown_signal)
  84.  
  85. io_loop.start()
  86.  
  87. log_stream.close()
  88. ctx.term()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement