Package: python-devlog
devlogNo more logging in your code business logic with python decorators.Logging is a very powerful tool for debugging and monitoring your code. But if you are often ommitting logging
statements, you will quickly find yourself overcrowded with logging statements.Fortunately, you could avoid this by using the python decorator. This library provide easy logging for your code without
stealing readability and maintainability. Furthermore, it also provides stack trace to get full local variable on the
function.How to use:To use this library, you just need to add the decorator to your function. Depending on when you want to log, you can use
the decorator as below:importloggingfromdevlogimportlog_on_start,log_on_end,log_on_errorlogging.basicConfig(level=logging.DEBUG)@log_on_start@log_on_enddeftest_1(a,b):returna+b@log_on_errordeftest_2(a,b):returna/bif__name__=='__main__':test_1(1,b=2)# INFO:__main__:Start func test_1 with args (1,), kwargs {'b': 2}# INFO:__main__:Successfully run func test_1 with args (1,), kwargs {'b': 2}test_2("abc","def")# ERROR:__main__:Error in func test_2 with args ('abc', 'def'), kwargs {}# 	unsupported operand type(s) for /: 'str' and 'str'.What devlog can do for youDecoratorsdevlog provides three different decorators:LogOnStart: Log when the function is called.LogOnEnd: Log when the function is finished.LogOnError: Log when the function is finished with error.Use variables in messagesThe message given to decorators are treated as a format string which takes the function arguments as the format
arguments.The following example shows how to use variables in messages:importloggingfromdevlogimportlog_on_startlogging.basicConfig(level=logging.DEBUG)@log_on_start(logging.INFO,'Start func{callable.__name__}with args{args}, kwargs{kwargs}')defhello(name):print("Hello,{}".format(name))if__name__=="__main__":hello("World")Which will print:INFO:__main__:Start func hello with args ('World',), kwargs {}Documentationformat variablesThe following variables are available in the format string:Default variable nameDescriptionLogOnStartLogOnEndLogOnErrorcallableThe function objectYesYesYesargs/kwargsThe arguments, key arguments passed to the functionYesYesYesresultThe return value of the functionNoYesNoerrorThe error object if the function is finished with errorNoNoYesbase argumentsAvailable arguments in all decorators are:ArgumentsDescriptionloggerThe logger object. If no logger is given, the devlog will create a logger object with the name of the module where the function is defined. Default islogging.getLogger(callable.__name__)handlerA custom log handler object. Only available if no logger object is given,args_kwargsSet true if the message format using args, kwargs format or false to use function parameter name format. DefaultTruecallable_format_variableThe format variable to use for callable. Default iscallabletrace_stackSet to True if you want to get the full stack trace. Default isFalseorcapture_localcapture_localsSet to True if you want to get the local variable of the function. Default isFalse(ortrace_stackon log_on_error decorator)include_decoratorSet to True if you want to include the devlog libray on the stack. Default isFalselog_on_startArgumentsDescriptionlevelThe level of the log message. Default islogging.INFOmessageThe message to log. Could using {args} {kwargs} or function parameter name but not both.Default isStart func {callable.__name__} with args {args}, kwargs {kwargs}log_on_endArgumentsDescriptionlevelThe level of the log message. Default islogging.INFOmessageThe message to log. Could using {args} {kwargs} or function parameter name but not both.Default isSuccessfully run func {callable.__name__} with args {args}, kwargs {kwargs}result_format_variableThe format variable to use for reference the return from callable. Default isresultlog_on_errorArgumentsDescriptionlevelThe level of the log message. Default islogging.ERRORmessageThe message to log. Could using {args} {kwargs} or function parameter name but not both.Default isSuccessfully run func {callable.__name__} with args {args}, kwargs {kwargs}on_exceptionsA tuple containing exception classes or a single exception, which should get caught and trigger the logging. Default istuple()(All exception will get caught)reraiseControl whether the exception should be reraised after logging. Default isTrueexception_format_variableThe format variable to use for reference the exception raised in callable. Default iserrorExtrasStack traceThe stack trace configuration.methodDescriptionset_stack_start_framesThe number of frames to skip at the start (positive value). Default is0set_stack_removal_framesThe number of frames to remove from the back of the stack trace (positive value). Default is6(the last 6 frame usually from the devlog module)Custom exception hookOverride the default exception hook with a custom function.importdevlogdevlog.system_excepthook_overwrite()# Overwrite the default exception hookThis will replace the sys.excepthook with the devlog.excepthook.ArgumentsDescriptionout_fileThe path to the file to write the log. Default iscrash.log