unicode syslogger, with no BOM

This commit is contained in:
Andy McKay 2011-11-04 17:10:08 -07:00
Родитель 2694f9a5d9
Коммит 321d8e4e5a
1 изменённых файлов: 28 добавлений и 3 удалений

Просмотреть файл

@ -1,5 +1,6 @@
import logging
import logging.handlers
import socket
from django.conf import settings
@ -15,6 +16,30 @@ class NullHandler(logging.Handler):
pass
class UnicodeLogger(logging.handlers.SysLogHandler):
def emit(self, record):
msg = self.format(record) + '\000'
prio = '<%d>' % self.encodePriority(self.facility,
self.mapPriority(record.levelname))
if type(msg) is unicode:
msg = msg.encode('utf-8')
msg = prio + msg
try:
if self.unixsocket:
try:
self.socket.send(msg)
except socket.error:
self._connect_unixsocket(self.address)
self.socket.send(msg)
else:
self.socket.sendto(msg, self.address)
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
base_fmt = ('%(name)s:%(levelname)s %(message)s '
':%(pathname)s:%(lineno)s')
@ -51,17 +76,17 @@ cfg = {
'formatter': 'debug',
},
'syslog': {
'()': logging.handlers.SysLogHandler,
'()': UnicodeLogger,
'facility': logging.handlers.SysLogHandler.LOG_LOCAL7,
'formatter': 'prod',
},
'syslog2': {
'()': logging.handlers.SysLogHandler,
'()': UnicodeLogger,
'facility': logging.handlers.SysLogHandler.LOG_LOCAL7,
'formatter': 'prod2',
},
'syslog_csp': {
'()': logging.handlers.SysLogHandler,
'()': UnicodeLogger,
'facility': logging.handlers.SysLogHandler.LOG_LOCAL5,
'formatter': 'csp',
},