diff options
author | Marko Kreen | 2012-04-05 11:04:26 +0000 |
---|---|---|
committer | Marko Kreen | 2012-04-05 11:04:26 +0000 |
commit | 899dca48051ba988d33cc4fa556016bde08876d7 (patch) | |
tree | 9432e52e64bfcfb5febd06701564fd7e30a0c64e | |
parent | 51c1c965d1242a8c7781ae00e5e9e0ca4ef975ff (diff) |
skylog: stop using standard SysLogHandler, it's broken
It fails to handle unicode strings - it puts BOM in
wrong place. Wrap logging.handlers implementation
in class that simply drops the BOM addition.
-rw-r--r-- | python/conf/skylog.ini | 2 | ||||
-rw-r--r-- | python/skytools/skylog.py | 45 |
2 files changed, 45 insertions, 2 deletions
diff --git a/python/conf/skylog.ini b/python/conf/skylog.ini index 5e3531b4..af885377 100644 --- a/python/conf/skylog.ini +++ b/python/conf/skylog.ini @@ -78,7 +78,7 @@ args=('~/log/%(job_name)s.log', 100*1024*1024, 3) formatter=long [handler_syslog] -class=handlers.SysLogHandler +class=skylog.SysLogHandler args=(('localhost', 514),) formatter=syslog diff --git a/python/skytools/skylog.py b/python/skytools/skylog.py index 330de053..354ef916 100644 --- a/python/skytools/skylog.py +++ b/python/skytools/skylog.py @@ -232,7 +232,48 @@ class LogDBHandler(logging.handlers.SocketHandler): query = "select * from log.add(%s, %s, %s)" logcur.execute(query, [type, service, msg]) -# send messages to syslog +# fix unicode bug in SysLogHandler +class SysLogHandler(logging.handlers.SysLogHandler): + """Fixes unicode bug in logging.handlers.SysLogHandler.""" + + def emit(self, record): + """ + Emit a record. + + The record is formatted, and then sent to the syslog server. If + exception information is present, it is NOT sent to the server. + """ + msg = self.format(record) + '\000' + """ + We need to convert record level to lowercase, maybe this will + change in the future. + """ + prio = '<%d>' % self.encodePriority(self.facility, + self.mapPriority(record.levelname)) + # Message is a string. Convert to bytes as required by RFC 5424 + if type(msg) is unicode: + msg = msg.encode('utf-8') + ## this puts BOM in wrong place + #if codecs: + # msg = codecs.BOM_UTF8 + msg + msg = prio + msg + try: + if self.unixsocket: + try: + self.socket.send(msg) + except socket.error: + self._connect_unixsocket(self.address) + self.socket.send(msg) + elif self.socktype == socket.SOCK_DGRAM: + self.socket.sendto(msg, self.address) + else: + self.socket.sendall(msg) + except (KeyboardInterrupt, SystemExit): + raise + except: + self.handleError(record) + + class SysLogHostnameHandler(logging.handlers.SysLogHandler): """Slightly modified standard SysLogHandler - sends also hostname and service type""" @@ -243,6 +284,8 @@ class SysLogHostnameHandler(logging.handlers.SysLogHandler): _hostname, _service_name, msg) + if type(msg) is unicode: + msg = msg.encode('utf-8') try: if self.unixsocket: try: |