Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
99 lines (72 sloc) 3.05 KB
# 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 """
# Add file handler
if logfilename != None:
if syslog:
if console:
# Add formatter
for handle in log.handlers:
formatter = logging.Formatter('%(asctime)s : %(levelname)-8s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
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)
# Add handlers
if console:
if filename != None:
# 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')
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)"Starting application...")"Initializing objects.")
time.sleep(14)"Initialization complete.")"Loading configuration and data ...")
time.sleep(115)'Loading complete. Listening for connections ...')
You can’t perform that action at this time.