Skip to main content

Stack log messages

Project description

PyPI Shield Downloads Travis CI Shield


Stack log messages


Stacklog is a tiny Python library to stack log messages.

A stack-structured log is an approach to logging in which log messages are (conceptually) pushed onto a stack and emitted only when the pusher returns. Stacklog provides a single method, stacklog, which serves as either a decorator or a context manager. This is exceptionally useful in small projects or one-off scripts.

This is illustrated best with an example:

with stacklog(print, 'Running some code'):
    with stacklog(print, 'Running some other code'):

This produces the following logging output:

Running some code...
Running some other code...
Running some other code...DONE
Running some code...DONE

When the code within a stacklog context completes, the provided message is echoed along with the return status, one of DONE or FAILURE. That's pretty much it.


stacklog has been developed and tested on Python 2.7 and 3.4+.

pip install stacklog


How often do you find yourself using the following logging anti-pattern in Python?

import logging

def a():'Running a')
    do_something()'Done with a')

def b():'Running b')
    a()'Done with b')

except:'There was an error running b')

The intention here is to log the beginning and end of procedure calls for use in debugging or user monitoring. I call this an anti-pattern because:

  • it requires excessive manual attention to writing/updating logging calls at entry/exit sites
  • it results in redundant exception handling logic
  • the resulting log messages can be misleading if errors occur

Instead, the approach taken by stacklog is to accomplish this using only decorators and context managers.

Usage as decorator

Here is the above example using the stacklog as a decorator:

@stacklog(, 'Running a')
def a():
    raise Exception

@stacklog(, 'Running b')
def b():


This produces logging output:

INFO:root:Running b...
INFO:root:Running a...
INFO:root:Running a...FAILURE
INFO:root:Running b...FAILURE

Usage as context manager

Here is another example using stacklog as a context manager:

>>> with stacklog(, 'Running some code'):
...     do_something()
INFO:root:Running some code...
INFO:root:Running some code...DONE

Providing custom conditions

A condition is a tuple exception, status. If the provided exception is raised during the execution of the provided code, the provided status is logged instead of the default FAILURE.

>>> with stacklog(, 'Running some code', conditions=[(NotImplementedError,
...     raise NotImplementedError
INFO:root:Running some code...
INFO:root:Running some code...SKIPPED


1.0 (2019-12-10)

Initial release.

Project details

Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for stacklog, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size stacklog-1.0.0-py2.py3-none-any.whl (4.9 kB) File type Wheel Python version py2.py3 Upload date Hashes View hashes
Filename, size stacklog-1.0.0.tar.gz (12.7 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page