3 ## Copyright 2007, Red Hat, Inc
6 ## This software may be freely redistributed under the terms of the GNU
7 ## general public license.
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.
17 from config
import read_config
18 from commonconfig
import CMConfig
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
28 # logging is weird, we don't want to setup multiple handlers
29 # so make sure we do that mess only once
31 class Logger(Singleton
):
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
]
40 self
._setup
_handlers
(logfilepath
=logfilepath
)
42 def _setup_logging(self
):
43 self
.logger
= logging
.getLogger("certmaster")
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
54 class AuditLogger(Singleton
):
56 def __init__(self
, logfilepath
= "/var/log/certmaster/audit.log"):
57 self
.logfilepath
= logfilepath
58 self
.loglevel
= logging
.INFO
61 self
._setup
_handlers
(logfilepath
=logfilepath
)
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
))
68 def _setup_logging(self
):
69 self
.logger
= logging
.getLogger("certmaster-audit")
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