Révision 26e35159
Ajouté par Benjamin Dauvergne il y a presque 11 ans
entrouvert/logging/handlers.py | ||
---|---|---|
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 |
def emit(self, record): |
|
10 |
""" |
|
11 |
Emit a record. |
|
12 |
|
|
13 |
The record is formatted, and then sent to the syslog server. If |
|
14 |
exception information is present, it is NOT sent to the server. |
|
15 |
""" |
|
16 |
source_msg = self.format(record) |
|
17 |
""" |
|
18 |
We need to convert record level to lowercase, maybe this will |
|
19 |
change in the future. |
|
20 |
""" |
|
21 |
prio = '<%d>' % self.encodePriority(self.facility, |
|
22 |
self.mapPriority(record.levelname)) |
|
23 |
max_length = 70 |
|
24 |
i = 0 |
|
25 |
while source_msg[i:]: |
|
26 |
msg = source_msg[i:i+max_length] + '\000' |
|
27 |
if i: |
|
28 |
msg = ' ' + msg |
|
29 |
i += max_length |
|
30 |
# Message is a string. Convert to bytes as required by RFC 5424 |
|
31 |
if type(msg) is unicode: |
|
32 |
msg = msg.encode('utf-8') |
|
33 |
if codecs: |
|
34 |
msg = codecs.BOM_UTF8 + msg |
|
35 |
msg = prio + msg |
|
36 |
try: |
|
37 |
if self.unixsocket: |
|
38 |
try: |
|
39 |
self.socket.send(msg) |
|
40 |
except socket.error: |
|
41 |
self._connect_unixsocket(self.address) |
|
42 |
self.socket.send(msg) |
|
43 |
elif self.socktype == socket.SOCK_DGRAM: |
|
44 |
self.socket.sendto(msg, self.address) |
|
45 |
else: |
|
46 |
self.socket.sendall(msg) |
|
47 |
except (KeyboardInterrupt, SystemExit): |
|
48 |
raise |
|
49 |
except: |
|
50 |
self.handleError(record) |
Formats disponibles : Unified diff
logging: add logging handler SysLogHandler