Skip to content
Informative Tracebacks for Python logging module
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Informative Traceback Logging for Python

  • Enable informative stack traces if you are already using the standard logging module: import rich_traceback.enable import logging

That's it, you can use logging as usual, if an exception happens it will be reported via configured root logger. You can also report exceptions via .exception()

  • Save this to and execute:
from rich_traceback.formatter import RichTracebackFormatter
import logging

logger = logging.getLogger('root')
console_log = logging.StreamHandler()

def foo(x=3):
    if 1.0/x:
    logger.exception("error running foo")
  • Exception traces are generated automatically for uncaught exceptions via an exception hook, or can be sent explicitly by calling Log.stackTrace() from inside an exception handler. The format is one frame per line, the order is reversed from the usual tracebacks so the exception is shown first.

$ python

ERROR root test.<module>:15 error running foo
<type 'exceptions.ZeroDivisionError'>: ZeroDivisionError('float division by zero',) ([5] frames following)
[4], foo(x=0) at line 9: if 1.0/x:
[3], foo(x=1) at line 10: foo(x-1)
[2], foo(x=2) at line 10: foo(x-1)
[1], foo(x=3) at line 10: foo(x-1)
[0], <module>() at line 13: foo()

Syslog traces would get the date and PID as shown below.

  • Simple standalone logger with console syslog support.


This will emit the following: test: init:6 got here

And in /var/log/messages (via syslog): Oct 11 22:17:00 m4600 test[24621]: init:6 got here

-- This work is based on the module published with this occasion:

You can’t perform that action at this time.