~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/twisted/python/syslog.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

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