b2d14ed8d8de2a1f3b26e10b4881a379cee2a328
[certmaster.git] / certmaster / logger.py
1 ## Certmaster
2 ##
3 ## Copyright 2007, Red Hat, Inc
4 ## See AUTHORS
5 ##
6 ## This software may be freely redistributed under the terms of the GNU
7 ## general public license.
8 ##
9 ## You should have received a copy of the GNU General Public License
10 ## along with this program; if not, write to the Free Software
11 ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
12 ##
13 ##
14
15
16 import logging
17 from config import read_config
18 from commonconfig import CMConfig
19
20
21 # from the comments in http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531
22 class Singleton(object):
23 def __new__(type, *args, **kwargs):
24 if not '_the_instance' in type.__dict__:
25 type._the_instance = object.__new__(type, *args, **kwargs)
26 return type._the_instance
27
28 # logging is weird, we don't want to setup multiple handlers
29 # so make sure we do that mess only once
30
31 class Logger(Singleton):
32 _no_handlers = True
33
34 def __init__(self, logfilepath ="/var/log/certmaster/certmaster.log"):
35 config_file = '/etc/certmaster/minion.conf'
36 self.config = read_config(config_file, CMConfig)
37 self.loglevel = logging._levelNames[self.config.log_level]
38 self._setup_logging()
39 if self._no_handlers:
40 self._setup_handlers(logfilepath=logfilepath)
41
42 def _setup_logging(self):
43 self.logger = logging.getLogger("svc")
44
45 def _setup_handlers(self, logfilepath="/var/log/certmaster/certmaster.log"):
46 handler = logging.FileHandler(logfilepath, "a")
47 self.logger.setLevel(self.loglevel)
48 formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
49 handler.setFormatter(formatter)
50 self.logger.addHandler(handler)
51 self._no_handlers = False
52
53
54 class AuditLogger(Singleton):
55 _no_handlers = True
56 def __init__(self, logfilepath = "/var/log/certmaster/audit.log"):
57 self.loglevel = logging.INFO
58 self._setup_logging()
59 if self._no_handlers:
60 self._setup_handlers(logfilepath=logfilepath)
61
62 def log_call(self, ip, CN, cert_hash, method, params):
63 # square away a good parseable format at some point -akl
64 self.logger.info("%s %s %s %s called with %s" % (ip, CN, cert_hash, method, params))
65
66
67 def _setup_logging(self):
68 self.logger = logging.getLogger("audit")
69
70 def _setup_handlers(self, logfilepath="/var/log/certmaster/audit.log"):
71 handler = logging.FileHandler(logfilepath, "a")
72 self.logger.setLevel(self.loglevel)
73 formatter = logging.Formatter("%(asctime)s - %(message)s")
74 handler.setFormatter(formatter)
75 self.logger.addHandler(handler)
76 self._no_handlers = False