-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathcustom_logger.py
98 lines (72 loc) · 3.05 KB
/
custom_logger.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Code Listing #9
"""
Custom loggers - simple logger object and a custom Logger class logging time spent in functions
"""
import logging
import logging.handlers
import time
from functools import partial
def create_logger(app_name, logfilename=None, level=logging.INFO,
console=False, syslog=False):
""" Build and return a custom logger. Accepts the application name,
log filename, loglevel and console logging toggle and syslog toggle """
log=logging.getLogger(app_name)
log.setLevel(logging.DEBUG)
# Add file handler
if logfilename != None:
log.addHandler(logging.FileHandler(logfilename))
if syslog:
log.addHandler(logging.handlers.SysLogHandler(address='/dev/log'))
if console:
log.addHandler(logging.StreamHandler())
# Add formatter
for handle in log.handlers:
formatter = logging.Formatter('%(asctime)s : %(levelname)-8s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
handle.setFormatter(formatter)
return log
class LoggerWrapper(object):
""" A wrapper class for logger objects with
calculation of time spent in each step """
def __init__(self, app_name, filename=None, level=logging.INFO, console=False):
self.log = logging.getLogger(app_name)
self.log.setLevel(level)
# Add handlers
if console:
self.log.addHandler(logging.StreamHandler())
if filename != None:
self.log.addHandler(logging.FileHandler(filename))
# Set formatting
for handle in self.log.handlers:
formatter = logging.Formatter('%(asctime)s [%(timespent)s]: %(levelname)-8s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
handle.setFormatter(formatter)
for name in ('debug','info','warning','error','critical'):
# Creating convenient wrappers by using functools
func = partial(self._dolog, name)
# Set on this class as methods
setattr(self, name, func)
# Mark timestamp
self._markt = time.time()
def _calc_time(self):
""" Calculate time spent so far """
tnow = time.time()
tdiff = int(round(tnow - self._markt))
hr, rem = divmod(tdiff, 3600)
mins, sec = divmod(rem, 60)
# Reset mark
self._markt = tnow
return '%.2d:%.2d:%.2d' % (hr, mins, sec)
def _dolog(self, levelname, msg, *args, **kwargs):
""" Generic method for logging at different levels """
logfunc = getattr(self.log, levelname)
return logfunc(msg, *args, extra={'timespent': self._calc_time()})
if __name__ == "__main__":
log=LoggerWrapper('myapp', filename='myapp.log',console=True)
log.info("Starting application...")
log.info("Initializing objects.")
time.sleep(14)
log.info("Initialization complete.")
log.info("Loading configuration and data ...")
time.sleep(115)
log.info('Loading complete. Listening for connections ...')