clogger.py 1.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Basic logger, storing data in /var/log/clogger/*.log
# Author    : Pierre-Elliott Bécue <becue@crans.org>
# License   : GPLv3
# Date      : 27/04/2014

import logging

class CLogger(logging.Logger):
    """
    Crans logger
    """

16
    def __init__(self, loggerName, service, level, debug=False):
17
        """
18
        Initializes logger. The debug variable is useful to have a print to stdout (when debugging)
19 20 21 22 23 24
        """
        super(CLogger, self).__init__(loggerName)

        # Creates FileHandler
        self.fh = logging.FileHandler("/var/log/clogger/%s.log" % (loggerName,))

25 26
        # Catches appropriate level in logging.
        self.fhlevel = getattr(logging, level.upper(), logging.INFO)
27 28 29
        self.fh.setLevel(self.fhlevel)

        # Creates formatter
30
        self.formatter = logging.Formatter('%%(asctime)s - %%(name)s - %(service)s - %%(levelname)s - %%(message)s' % {'service': service})
31 32 33 34

        # Adds formatter to FileHandler
        self.fh.setFormatter(self.formatter)

35 36 37 38 39 40 41
        if debug:
            self.sh = logging.StreamHandler()
            self.shlevel = logging.DEBUG
            self.sh.setLevel(self.shlevel)
            self.sh.setFormatter(self.formatter)
            self.addHandler(self.sh)

42 43
        # Adds FileHandler to Handlers
        self.addHandler(self.fh)