Project

General

Profile

Download (1.8 KB) Statistics
| Branch: | Tag: | Revision:

root / entrouvert / logging / handlers.py @ master

1
import logging.handlers
2
import socket
3
try:
4
    import codecs
5
except ImportError:
6
    codecs = None
7

    
8
class SysLogHandler(logging.handlers.SysLogHandler):
9
    max_length = 120
10

    
11
    def __init__(self, *args, **kwargs):
12
        self.max_length = kwargs.pop('max_length', self.max_length)
13
        logging.handlers.SysLogHandler.__init__(self, *args, **kwargs)
14

    
15
    def emit(self, record):
16
        """
17
        Emit a record.
18

    
19
        The record is formatted, and then sent to the syslog server. If
20
        exception information is present, it is NOT sent to the server.
21
        """
22
        source_msg = self.format(record)
23
        """
24
        We need to convert record level to lowercase, maybe this will
25
        change in the future.
26
        """
27
        prio = '<%d>' % self.encodePriority(self.facility,
28
                                            self.mapPriority(record.levelname))
29
        i = 0
30
        while source_msg[i:]:
31
            msg = source_msg[i:i+self.max_length] + '\000'
32
            if i:
33
                msg = ' ' + msg
34
            i += self.max_length
35
            # Message is a string. Convert to bytes as required by RFC 5424
36
            if type(msg) is unicode:
37
                msg = msg.encode('utf-8')
38
            msg = prio + msg
39
            try:
40
                if self.unixsocket:
41
                    try:
42
                        self.socket.send(msg)
43
                    except socket.error:
44
                        self._connect_unixsocket(self.address)
45
                        self.socket.send(msg)
46
                elif self.socktype == socket.SOCK_DGRAM:
47
                    self.socket.sendto(msg, self.address)
48
                else:
49
                    self.socket.sendall(msg)
50
            except (KeyboardInterrupt, SystemExit):
51
                raise
52
            except:
53
                self.handleError(record)
(3-3/3)