/IPython/parallel/apps/iploggerapp.py

https://github.com/matthagy/ipython · Python · 103 lines · 57 code · 20 blank · 26 comment · 3 complexity · b351d91a1432aed80e19b03449bcbf15 MD5 · raw file

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. A simple IPython logger application
  5. Authors:
  6. * MinRK
  7. """
  8. #-----------------------------------------------------------------------------
  9. # Copyright (C) 2011 The IPython Development Team
  10. #
  11. # Distributed under the terms of the BSD License. The full license is in
  12. # the file COPYING, distributed as part of this software.
  13. #-----------------------------------------------------------------------------
  14. #-----------------------------------------------------------------------------
  15. # Imports
  16. #-----------------------------------------------------------------------------
  17. import os
  18. import sys
  19. import zmq
  20. from IPython.core.profiledir import ProfileDir
  21. from IPython.utils.traitlets import Bool, Dict, Unicode
  22. from IPython.parallel.apps.baseapp import (
  23. BaseParallelApplication,
  24. base_aliases,
  25. catch_config_error,
  26. )
  27. from IPython.parallel.apps.logwatcher import LogWatcher
  28. #-----------------------------------------------------------------------------
  29. # Module level variables
  30. #-----------------------------------------------------------------------------
  31. #: The default config file name for this application
  32. default_config_file_name = u'iplogger_config.py'
  33. _description = """Start an IPython logger for parallel computing.
  34. IPython controllers and engines (and your own processes) can broadcast log messages
  35. by registering a `zmq.log.handlers.PUBHandler` with the `logging` module. The
  36. logger can be configured using command line options or using a cluster
  37. directory. Cluster directories contain config, log and security files and are
  38. usually located in your ipython directory and named as "profile_name".
  39. See the `profile` and `profile-dir` options for details.
  40. """
  41. #-----------------------------------------------------------------------------
  42. # Main application
  43. #-----------------------------------------------------------------------------
  44. aliases = {}
  45. aliases.update(base_aliases)
  46. aliases.update(dict(url='LogWatcher.url', topics='LogWatcher.topics'))
  47. class IPLoggerApp(BaseParallelApplication):
  48. name = u'iplogger'
  49. description = _description
  50. config_file_name = Unicode(default_config_file_name)
  51. classes = [LogWatcher, ProfileDir]
  52. aliases = Dict(aliases)
  53. @catch_config_error
  54. def initialize(self, argv=None):
  55. super(IPLoggerApp, self).initialize(argv)
  56. self.init_watcher()
  57. def init_watcher(self):
  58. try:
  59. self.watcher = LogWatcher(config=self.config, log=self.log)
  60. except:
  61. self.log.error("Couldn't start the LogWatcher", exc_info=True)
  62. self.exit(1)
  63. self.log.info("Listening for log messages on %r"%self.watcher.url)
  64. def start(self):
  65. self.watcher.start()
  66. try:
  67. self.watcher.loop.start()
  68. except KeyboardInterrupt:
  69. self.log.critical("Logging Interrupted, shutting down...\n")
  70. def launch_new_instance():
  71. """Create and run the IPython LogWatcher"""
  72. app = IPLoggerApp.instance()
  73. app.initialize()
  74. app.start()
  75. if __name__ == '__main__':
  76. launch_new_instance()