Package: logex
ContentsSettingsExampleLogex is a python module to easily add logging for unhandled exceptions in
D-Bus, thread and other functions.
It can also be quite helpful when developing with the new asyncio module of
python 3.4. Although this module does some sort of exception logging, it can
easily happen that exceptions are accidentally swallowed.
Although unhandled exceptions get written to STDERR by default and most modules
provide some mechanism to log these, this is not always sufficient, e.g. when
inside a daemon which discards all default output.
Sometimes it may also be desirable to automatically send an email if some
exception occurs or at least write some kind of audit log.This modlue comes with a decorator function which can be applied on demand. It
provides advanced debugging information which gives you the most relevant
information for each frame in the exception’s traceback.SettingsA description of all available settings is available in the source
documentation of thelog()function in thelogex.pymodule.All settings can be set “globally”, e.g. by using:importlogexlogex.VIEW_SOURCE=Trueor they can be specified when decorating a function or method:importlogex@logex.log(view_source=True)deff():passCurrently available module variables and their defaults:LOGFUNCTION = logging.errorTEMPLATE = ('Unhandled exception calling%(funcname)s(%(argsview)s):\n%(traceback)s\n%(sourceview)s')ADVANCED = FalseLAZY = FalseRERAISE = TrueCATCHALL = FalseVIEW_SOURCE = FalseDETECT_NESTED = TrueThe default behaviour of the log() decorator is to generate a log message usingTEMPLATEand pass this toLOGFUNCTION.
For a list of place holders that are replaced, see thegenerate_log_message()function inlogex.py.SettingADVANCEDto True, gives you the opportunity to use a logging function
which interprets the exception data itself. For a description of the arguments
to an advanced logging function, see thegenerate_log_message()function inlogex.py.IfLAZYis True, the logging function itself must return another function
object which is then used as the actual logging function.RERAISEcan be set to False to only log the exception and continue “normally”.
This will, of course, not resume the decorated function itself, but return to
the caller of this function and resume execution there.IfCATCHALLis True, all exceptions are caught, i.e. also KeyboardInterrupt,
SystemExit and GeneratorExit.VIEW_SOURCEcan be used to show the source code and local variables for every
function in the exceptions traceback.logex will usually detect if an exception occurs in a logex-decorated function
which, in turn, was called by a logex-decorated function and shorten the output
so you do not get the same source view twice.DETECT_NESTEDcan be used to
disable this feature and always print the full source view.ExampleSeveral examples can be found in theexamplesdirectory, so we show just some
simple usage here:#!/usr/bin/pythonimportlogex@logex.logdefmy_function():raiseException("something bad happens here")my_function()This will simply print the exception traceback, like this:ERROR:root:Unhandledexceptioncallingmy_function():Traceback(mostrecentcalllast):File"./x.py",line10,inmy_functiondo_something_dangerous()File"./x.py",line5,indo_something_dangerousraiseException("something bad happens here")Exception:somethingbadhappenshereTraceback(mostrecentcalllast):File"./x.py",line13,in<module>my_function()File"/home/tobi/repos/logex/logex.py",line318,inwrapper_ftemplate,view_source,reraise,wrapper_code=wrapper_code)File"/home/tobi/repos/logex/logex.py",line306,inwrapper_fwrapped_f(*args,**kwargs)File"./x.py",line10,inmy_functiondo_something_dangerous()File"./x.py",line5,indo_something_dangerousraiseException("something bad happens here")Exception:somethingbadhappenshereThe second traceback is the one generated because an unhandled exception
occurs(logex reraises exceptions by default), it also contains some extra
frames generated by the logex decorator.
Let’s make it a bit more advanced and pleasant to read:#!/usr/bin/pythonimportlogex@logex.log(view_source=True,reraise=False)defmy_function():x=1raiseException("something bad happens here")my_function()This yields:ERROR:root:Unhandledexceptioncallingmy_function():Traceback(mostrecentcalllast):File"./x.py",line10,inmy_functiondo_something_dangerous()File"./x.py",line5,indo_something_dangerousraiseException("something bad happens here")Exception:somethingbadhappenshere==========sourcecode==========---------------------------./x.py:my_function---------------------------7@logex.log(view_source=True,reraise=False)8defmy_function():9x=110-->do_something_dangerous()...Localswhenexecutingline10:*x:1--------------------------------------./x.py:do_something_dangerous--------------------------------------4defdo_something_dangerous():5-->raiseException("something bad happens here")================================We have several differences here:the exception is not reraised, this is probably not always desired, but makes
some nicer output here ;)a view of the sourcecode for each function in the tracebacka list of the current values for local variables, if present