syslog.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- test-case-name: twisted.python.test.test_syslog -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Classes and utility functions for integrating Twisted and syslog.
  6. You probably want to call L{startLogging}.
  7. """
  8. syslog = __import__('syslog')
  9. from twisted.python import log
  10. from twisted.python._oldstyle import _oldStyle
  11. # These defaults come from the Python syslog docs.
  12. DEFAULT_OPTIONS = 0
  13. DEFAULT_FACILITY = syslog.LOG_USER
  14. @_oldStyle
  15. class SyslogObserver:
  16. """
  17. A log observer for logging to syslog.
  18. See L{twisted.python.log} for context.
  19. This logObserver will automatically use LOG_ALERT priority for logged
  20. failures (such as from C{log.err()}), but you can use any priority and
  21. facility by setting the 'C{syslogPriority}' and 'C{syslogFacility}' keys in
  22. the event dict.
  23. """
  24. openlog = syslog.openlog
  25. syslog = syslog.syslog
  26. def __init__(self, prefix, options=DEFAULT_OPTIONS,
  27. facility=DEFAULT_FACILITY):
  28. """
  29. @type prefix: C{str}
  30. @param prefix: The syslog prefix to use.
  31. @type options: C{int}
  32. @param options: A bitvector represented as an integer of the syslog
  33. options to use.
  34. @type facility: C{int}
  35. @param facility: An indication to the syslog daemon of what sort of
  36. program this is (essentially, an additional arbitrary metadata
  37. classification for messages sent to syslog by this observer).
  38. """
  39. self.openlog(prefix, options, facility)
  40. def emit(self, eventDict):
  41. """
  42. Send a message event to the I{syslog}.
  43. @param eventDict: The event to send. If it has no C{'message'} key, it
  44. will be ignored. Otherwise, if it has C{'syslogPriority'} and/or
  45. C{'syslogFacility'} keys, these will be used as the syslog priority
  46. and facility. If it has no C{'syslogPriority'} key but a true
  47. value for the C{'isError'} key, the B{LOG_ALERT} priority will be
  48. used; if it has a false value for C{'isError'}, B{LOG_INFO} will be
  49. used. If the C{'message'} key is multiline, each line will be sent
  50. to the syslog separately.
  51. """
  52. # Figure out what the message-text is.
  53. text = log.textFromEventDict(eventDict)
  54. if text is None:
  55. return
  56. # Figure out what syslog parameters we might need to use.
  57. priority = syslog.LOG_INFO
  58. facility = 0
  59. if eventDict['isError']:
  60. priority = syslog.LOG_ALERT
  61. if 'syslogPriority' in eventDict:
  62. priority = int(eventDict['syslogPriority'])
  63. if 'syslogFacility' in eventDict:
  64. facility = int(eventDict['syslogFacility'])
  65. # Break the message up into lines and send them.
  66. lines = text.split('\n')
  67. while lines[-1:] == ['']:
  68. lines.pop()
  69. firstLine = True
  70. for line in lines:
  71. if firstLine:
  72. firstLine = False
  73. else:
  74. line = '\t' + line
  75. self.syslog(priority | facility,
  76. '[%s] %s' % (eventDict['system'], line))
  77. def startLogging(prefix='Twisted', options=DEFAULT_OPTIONS,
  78. facility=DEFAULT_FACILITY, setStdout=1):
  79. """
  80. Send all Twisted logging output to syslog from now on.
  81. The prefix, options and facility arguments are passed to
  82. C{syslog.openlog()}, see the Python syslog documentation for details. For
  83. other parameters, see L{twisted.python.log.startLoggingWithObserver}.
  84. """
  85. obs = SyslogObserver(prefix, options, facility)
  86. log.startLoggingWithObserver(obs.emit, setStdout=setStdout)