otwrapy.Debug¶
- class Debug(logger, loglevel=None)¶
Decorator that catches exceptions inside a function and logs them.
A decorator used to protect functions so that exceptions are logged to a file. It can either be instantiated with a Logger or with a filename for which a logger will be created with a FileHandler. It comes specially handy when you launch your codes in a non interactive environment (e.g., HPC cluster through submission scripts), given that Exceptions are captured and logged to a file.
The great benefit of this implementation is that with a simple decorator you can protect the methods of your Wrapper class with a try/except structure. However, this might not be useful for a deeper debugging where you want to have access to the locals() of the place where the Exception jumped. If you bump into such a case, add a try/except structure that catches the Exception on the specific place. It is advised to use the decorator once you have developed the wrapper and that you are ready to launch your uncertainty studies.
- Parameters:
- loggerlogging.Logger or str
Either a Logger instance or a filename for the logger to be created.
- loglevellogging level
Threshold for the logger. Logging messages which are less severe than loglevel will be ignored. It defaults to logging.DEBUG.
Methods
__call__
(func)Call self as a function.
Examples
To catch exceptions raised inside a function and log them to a file :
>>> import otwrapy as otw >>> @otw.Debug('func.log') >>> def func(*args, **kwargs): >>> pass
- __init__(logger, loglevel=None)¶
Methods
__init__
(logger[, loglevel])