summaryrefslogtreecommitdiffstats
path: root/minion/logger.py
blob: f6f9c0f3217da3e98c3208adb9ff807c5737eeb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/python

## func
##
## Copyright 2007, Red Hat, Inc
## See AUTHORS
##
## This software may be freely redistributed under the terms of the GNU
## general public license.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##
##


import logging
import config_data


# from the comments in http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531
class Singleton(object):
    def __new__(type, *args, **kwargs):
        if not '_the_instance' in type.__dict__:
            type._the_instance = object.__new__(type, *args, **kwargs)
        return type._the_instance

# logging is weird, we don't want to setup multiple handlers
# so make sure we do that mess only once

class Logger(Singleton):
    _no_handlers = True

    def __init__(self, logfilepath ="/var/log/func/func.log"):

        self.config = config_data.Config().get()     
        if self.config.has_key("log_level"):
           self.loglevel = logging._levelNames[self.config["log_level"]]
        else:
           self.loglevel = logging.INFO   
        self._setup_logging()
        if self._no_handlers:
            self._setup_handlers(logfilepath=logfilepath)
        
    def _setup_logging(self):
        self.logger = logging.getLogger("svc")

    def _setup_handlers(self, logfilepath="/var/log/func/func.log"):
        handler = logging.FileHandler(logfilepath, "a")
        self.logger.setLevel(self.loglevel)
        formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
        handler.setFormatter(formatter)
        self.logger.addHandler(handler)
        self._no_handlers = False


class AuditLogger(Singleton):
    _no_handlers = True
    def __init__(self, logfilepath = "/var/log/func/audit.log"):
        self.loglevel = logging.INFO
        self._setup_logging()
        if self._no_handlers:
             self._setup_handlers(logfilepath=logfilepath)

    def log_call(self, CN, cert_hash, method, params):
        # square away a good parseable format at some point -akl
        self.logger.info("%s %s %s called with %s" % (CN, cert_hash, method, params))


    def _setup_logging(self):
        self.logger = logging.getLogger("audit")

    def _setup_handlers(self, logfilepath="/var/log/func/audit.log"):
        handler = logging.FileHandler(logfilepath, "a")
        self.logger.setLevel(self.loglevel)
        formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
        handler.setFormatter(formatter)
        self.logger.addHandler(handler)
        self._no_handlers = False