__init__.py 723 Bytes
Newer Older
1
import logging
2

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
FORMAT = '%(asctime)s %(name)s %(levelname)s %(message)s'


class LogStashFormatter(logging.Formatter):
    def __init__(self, fmt=None):
        if not fmt:
            fmt = FORMAT
        logging.Formatter.__init__(self, fmt)

    def format(self, record):
        indent = " "
        msg = logging.Formatter.format(self, record).split("\n")
        return "\n".join([msg[0]] + [indent + l for l in msg[1:]])


def setupLogfile(logfile):
    handler = logging.handlers.RotatingFileHandler(logfile, maxBytes=10*1024**3,
                                                   backupCount=5)
    formatter = LogStashFormatter()
    handler.setFormatter(formatter)
    logging.getLogger("").addHandler(handler)