Package: loggingdecorators
loggingdecoratorsSimple, easy-to-use decorators for logging object initialisation and function calls using the
coreloggingmodule.This package provides 2 decorators which allow you to separate logging functionality
from business logic.Note that these decorators perform no logging setup, this is left to the user.on_initon_init(logger:typing.Union[str,logging.Logger,callable]="logger",level=logging.DEBUG,logargs=True,depth=0)When applied to a class or an__init__method, decorate it with a wrapper which logs the__init__call using the
given logger at the specified level.Ifloggeris a string, look up an attribute of this name in the initialised object and use it to log the message. Ifloggeris a function, call it to obtain a reference to a logger instance.
Otherwise, assumeloggeris an instance of a logger from the logging library and use it to log the message.IflogargsisTrue, the message contains the arguments passed to__init__.If the decorated class or__init__method is to be nested inside other decorators, increase the depth argument by 1
for each additional level of nesting in order for the messages emitted to contain the correct source file name &
line number.ExamplesApplied directly to a user-defined class:fromloggingdecoratorsimporton_initfromloggingimportgetLogger# as a class decorator...@on_init(logger=getLogger())classWidget:...classOtherWidget:# ... or as an __init__ decorator@on_init(logger=getLogger())def__init__(self):...Decorating a built-in class using a subclass:fromloggingdecoratorsimporton_initfromcollectionsimportdefaultdictfromloggingimportgetLogger@on_init(logger=getLogger())classdefaultdict_log(defaultdict):passNote:it is not recommended to directly decorate a built-in class unless you want
all initialisations of that class to be logged, as this decorator replaces the class'__init__method.Decorating a class in a subclass with a mixin:fromloggingdecoratorsimporton_initfromloggingimportgetLoggerclassWidget:...classLoggingMixin:@on_init(logger=getLogger())def__init__(self,*args,**kwargs):super().__init__(*args,**kwargs)classLoggingWidget(LoggingMixin,Widget):passon_callon_call(logger:typing.Union[logging.Logger,callable],level=logging.DEBUG,logargs=True,depth=0)When applied to a function, decorate it with a wrapper which logs the call using the given logger at the specified
level.Theloggerargument must be an instance of a logger from the logging library, or a function which returns an instance of a logger.IflogargsisTrue, log the function arguments, one per line.If the decorated function is to be nested inside other decorators, increase thedepthargument by 1 for each
additional level of nesting in order for the messages emitted to contain the correct source file name & line number.ExamplesDirectly decorating a function:fromloggingdecoratorsimporton_callfromloggingimportgetLogger@on_call(logger=getLogger())definteresting_function(*args,**kwargs):...interesting_function()Creating a logging version of another function:fromloggingdecoratorsimporton_callfromloggingimportgetLoggerdefinteresting_function(*args,**kwargs):...decorator=on_call(logger=getLogger())interesting_function_log=decorator(interesting_function)interesting_function_log()