be a bit more verbose in the logging here, add file location info to logs
[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("certmaster")
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.logfilepath = logfilepath
58 self.loglevel = logging.INFO
59 self._setup_logging()
60 if self._no_handlers:
61 self._setup_handlers(logfilepath=logfilepath)
62
63 def log_call(self, ip, method, params):
64 # square away a good parseable format at some point -akl
65 self.logger.info("%s called %s with %s" % (ip, method, params))
66
67
68 def _setup_logging(self):
69 self.logger = logging.getLogger("certmaster-audit")
70
71 def _setup_handlers(self, logfilepath="/var/log/certmaster/audit.log"):
72 handler = logging.FileHandler(logfilepath, "a")
73 self.logger.setLevel(self.loglevel)
74 formatter = logging.Formatter("%(asctime)s - %(message)s")
75 handler.setFormatter(formatter)
76 self.logger.addHandler(handler)
77 self._no_handlers = False