-
Notifications
You must be signed in to change notification settings - Fork 2
/
logit.py
66 lines (55 loc) · 1.91 KB
/
logit.py
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
"""
logit
A customer loging library that serves mostly as a thin wrapper for logging
allows customisation via config
@category Utility
@version $ID: 1.1.1, 2015-07-17 17:00:00 CST $;
@author KMR
@licence GNU GPL v.3
"""
import logging
class logit:
"""
logit class, extends logging will the option to send all logging to the cli
"""
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
max_lvl = None
handler = None
levels = {
'DEBUG': logging.DEBUG,
'INFO': logging.INFO,
'WARNING': logging.WARNING,
'ERROR': logging.ERROR,
'CRITICAL': logging.CRITICAL
}
def __init__(self, conf):
""" Initialise a new logit
:param conf: the config object
:return logit: a new instance of a logit
"""
self.max_lvl = conf['DEBUG']
if conf['DEBUG'] == 'Caveman':
print 'Caveman debug selected, printing everything'
else:
# setup the handler
handler = logging.FileHandler(conf['LOG_FILE'])
# set the logging level
handler.setLevel(self.levels[conf['DEBUG']])
# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
# add the handlers to the logger
self.logger.addHandler(handler)
self.logger.info('Logit initialised')
def __getattr__(self, name):
""" Generalised function call, covers all methods
:param name: the name of the function being called
"""
def wrapper(*args, **kwargs):
if self.max_lvl == 'Caveman':
print args[0]
else:
method_to_call = getattr(self.logger, name)
method_to_call(args[0])
return wrapper