Package: logging-decorator
LoggingDecoratorAboutLoggingDecorator is a MIT licensed general-purpose software package written in Python that simplyfies the task of logging methods of other Python scripts.Getting StartedTo install this package you can just type in your Python terminal:pipinstalllogging-decoratorRequirementsThis Python package only works withPython 3.6or later.UsageThe simplest use case is logging a generic method and displaying the log on screen.fromLoggingDecoratorimportlogd@logddeffoo():print('foo method')defmain():foo()if__name__=='__main__'main()FeaturesGenerate.logfiles inside alogfolder in the current working directory.fromLoggingDecoratorimportlogd@logd(file=True)deffoo():print('foo method')defmain():foo()if__name__=='__main__'main()Continue running the script if an exception is raised:fromLoggingDecoratorimportlogd@logd(raise_error=False)deffoo():print('foo method')defmain():foo()if__name__=='__main__'main()Set logging level:fromLoggingDecoratorimportlogd@logd(logging_level='critical')deffoo():print('foo method')defmain():foo()if__name__=='__main__'main()The available options are:critical,error,warning,info,debugandnotset.Output SampleSupose that you have afooproject with the following structure:📦foo
 ┗ 📜foo.pyAnd the contents of thefoo.pyfile is the following:fromtimeimportsleepfromLoggingDecoratorimportlogd@logd(file=True,logging_level='debug')deffoo_sum(a:int,b:int=1):sleep(3)returna+b@logd(file=False,raise_error=True)deffoo_div(a:int,b:int=0):returna/bdefmain():foo_sum(1)foo_div(5)if__name__=='__main__':main()The methodfoo_sumwill print and generate a file like./foo/log/foo_foo_sum_28Aug2021_114203.logwith the following content:2021-08-28 11:42:03,695 - Working directory:     /absolute/path/to/foo
2021-08-28 11:42:03,695 - Module name:           __main__
2021-08-28 11:42:03,696 - Method name:           foo_sum
2021-08-28 11:42:03,696 - Error message:         None
2021-08-28 11:42:03,696 - Start time:            2021-08-28 11:42:00.690282
2021-08-28 11:42:03,696 - End time:              2021-08-28 11:42:03.694016
2021-08-28 11:42:03,696 - Elapsed time:          0:00:03.003734
2021-08-28 11:42:03,697 - Variables:             (a: int, b: int = 1)
2021-08-28 11:42:03,697 - Args:                  None
2021-08-28 11:42:03,697 - Kwargs:                None
2021-08-28 11:42:03,697 - Log path:              /absolute/path/to/foo/log/foo_foo_sum_28Aug2021_114203.logAnd thefoo_divwill generate the following message in your screen:2021-08-28 11:42:03,698 - Module name:           __main__
2021-08-28 11:42:03,699 - Method name:           foo_div
2021-08-28 11:42:03,699 - Error message:         division by zero
2021-08-28 11:42:03,699 - Start time:            2021-08-28 11:42:03.698178
2021-08-28 11:42:03,699 - End time:              2021-08-28 11:42:03.698622
2021-08-28 11:42:03,699 - Elapsed time:          0:00:00.000444
2021-08-28 11:42:03,700 - Variables:             (a: int, b: int = 0)
2021-08-28 11:42:03,700 - Args:                  (5,)
2021-08-28 11:42:03,700 - Kwargs:                {}
2021-08-28 11:42:03,700 - Log path:              None
Traceback (most recent call last):
  File "/absolute/path/to/foo/foo.py", line 18, in <module>
    main()
  File "//absolute/path/to/foo/foo/foo.py", line 15, in main
    foo_div(5)
  File "/local/python/path/site-packages/LoggingDecorator/LoggingDecorator.py", line 33, in wrapped_function
    return LoggingDecorator(*dargs, **dkwargs).call(original_method, *args, **kwargs)
  File "/local/python/path/site-packages/LoggingDecorator/LoggingDecorator.py", line 78, in call
    return self._summary(original_method, 
  File "/local/python/path/site-packages/LoggingDecorator/LoggingDecorator.py", line 149, in _summary
    raise LogException(error_message)
LoggingDecorator.LoggingDecorator.LogException: LogException[division by zero]Authors@lkellermann- Idea & Initial work