Giter VIP home page Giter VIP logo

logstack's Introduction

logstack: Augments your stacktraces and logging by adding context

Build Status

Introduction

Python has the ability to display stacktraces when required (through the traceback module) or when exceptions occur. However, these only allow you to see the current path down the code that the execution took; what the application is doing, what it is doing it on, is always unclear.

Sure, you might want to fire up a debugger or log everything so that the context is in there somewhere. But this is very heavy.

This modules allows you to log context, like you would with logging.info(), and have this contextual information only pop up if an error occur. Furthermore, this information can be inlined into your stacktrace, augmenting the raw line numbers/method names with parameter values and custom messages.

Traceback (most recent call last):
  File "getting_started.py", line 19, in <module>
    foo()
  File "getting_started.py", line 17, in foo
  starting up (in_foo=True)
    baz()
  File "getting_started.py", line 13, in baz
  (in_baz=True)
    raise ValueError("ohno")
ValueError: ohno

Getting started

import logging, logstack

# Setup: you need to do this somewhere before using logging or logstack
#
# Setup logging to log to the console (cf logging documentation)
console = logging.StreamHandler()
logging.getLogger().addHandler(console)
#
# Use the Formatter from logstack, it will add context to stack traces
console.setFormatter(logstack.Formatter())
#

# Example, generates the traceback above
def bar():
    # Adds context; bar() is not on the stack when logging, so won't be shown
    logstack.push("doing stuff", in_bar=True)
def baz():
    logstack.push(in_baz=True) # relevant context
    with logstack.pushed(something=[1, 2, 3]): # context for debug(),
        logging.debug("in block")              # discarded by logging level
                                               # along with the regular message
    raise ValueError("ohno")
def foo():
    logstack.push("starting up", in_foo=True)  # relevant context
    bar()
    baz()
try:
    foo()
except:
    logging.critical("Got an exception!", exc_info=True)

logstack's People

Contributors

madjar avatar remram44 avatar

Watchers

 avatar

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.