Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

syslog.py 3.6KB

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