Skip to content

Commit

Permalink
added logger class
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed Oct 15, 2014
1 parent 55c92f5 commit 384d2f7
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions python_utils/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import logging
import functools

__all__ = ['Logged']


class Logged(object):
'''Class which automatically adds a named logger to your class when
interiting
Adds easy access to debug, info, warning, error, exception and log methods
>>> class MyClass(Logged):
... def __init__(self):
... Logged.__init__(self)
>>> my_class = MyClass()
>>> my_class.debug('debug')
>>> my_class.info('info')
>>> my_class.warning('warning')
>>> my_class.error('error')
>>> my_class.exception('exception')
>>> my_class.log(0, 'log')
'''
def __init__(self, *args, **kwargs):
self.logger = logging.getLogger(
self.__get_name(__name__, self.__class__.__name__))

def __get_name(self, *name_parts):
return '.'.join(n.strip() for n in name_parts if n.strip())

@functools.wraps(logging.debug)
def debug(self, msg, *args, **kwargs):
self.logger.debug(msg, *args, **kwargs)

@functools.wraps(logging.info)
def info(self, msg, *args, **kwargs):
self.logger.info(msg, *args, **kwargs)

@functools.wraps(logging.warning)
def warning(self, msg, *args, **kwargs):
self.logger.warning(msg, *args, **kwargs)

@functools.wraps(logging.error)
def error(self, msg, *args, **kwargs):
self.logger.error(msg, *args, **kwargs)

@functools.wraps(logging.exception)
def exception(self, msg, *args, **kwargs):
self.logger.exception(msg, *args, **kwargs)

@functools.wraps(logging.log)
def log(self, lvl, msg, *args, **kwargs):
self.logger.log(lvl, msg, *args, **kwargs)

0 comments on commit 384d2f7

Please sign in to comment.