Commit 0870575c authored by Anael Beutot's avatar Anael Beutot Committed by Antoine Millet
Browse files

Added a formatter class to handle unicode with syslog

parent 52fddbab
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
import logging


class EncodingFormatter(logging.Formatter, object):

    def __init__(self, fmt=None, datefmt=None, encoding='utf-8'):
        super(EncodingFormatter, self).__init__(fmt, datefmt)
        self._encoding = encoding

    def formatException(self, ei):
        expt = super(EncodingFormatter, self).formatException(ei)
        if isinstance(expt, str):
            expt = expt.decode(self._encoding, 'replace')
        return expt

    def format(self, record):
        msg = super(EncodingFormatter, self).format(record)
        if isinstance(msg, unicode):
            msg = msg.encode(self._encoding, 'replace')
        return msg