Git Product home page Git Product logo

rich-traceback's Introduction

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 test.py and execute:
from rich_traceback.formatter import RichTracebackFormatter
import logging

logger = logging.getLogger('root')
console_log = logging.StreamHandler()
console_log.setFormatter(RichTracebackFormatter())
logger.addHandler(console_log)

def foo(x=3):
    if 1.0/x:
        foo(x-1)
try:
    foo()
except:
    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 test.py

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

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

  • Simple standalone logger with console syslog support.

Log().debug('message')

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: https://mail.python.org/pipermail/python-list/2003-April/202381.html

rich-traceback's People

Contributors

ikari-pl avatar laurb9 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rich-traceback's Issues

Multiline tracebacks in syslog

A traceback multiline log message gets recorded as a single line with newlines replaced by #012.

Setting this in rsyslog.conf could prevent this but see if there's a better way, maybe convert to multiple log records individually tagged.

$EscapeControlCharactersOnReceive off

integration with standard logging module

Refactor module to use the standard logging module that was introduced in Python 2.3.
Offload syslog output to standard logging module.
Create Logger subclass with the new more informative stack traces.
Figure out an elegant way to enable the exception callback to report uncaught exceptions.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.