Logbook

1.9.2 · active · verified Thu Apr 09

Logbook is a Python library designed as a logging replacement, offering a fresh take on logging that is often considered more intuitive and powerful than Python's standard `logging` module. It focuses on clean, contextual logging and robust handler management. The current stable version is 1.9.2, with releases occurring intermittently based on contributions and maintenance.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic logging with Logbook using a `Logger` instance and a `StreamHandler` to output messages to standard output. It shows how to use a handler as a context manager (`with ... push_application():`) to ensure proper lifecycle management. Error logging with `log.exception` is also included.

from logbook import Logger, StreamHandler
import sys

log = Logger('MyApp')

def run_app():
    log.info('Application started.')
    log.debug('This is a debug message.')
    try:
        1 / 0
    except ZeroDivisionError:
        log.exception('An error occurred during calculation!')
    log.info('Application finished.')

if __name__ == '__main__':
    # Handlers should be pushed/popped or used as context managers
    with StreamHandler(sys.stdout, level='INFO').push_application():
        run_app()

    # Example with a file handler (ensure 'app.log' is writable)
    # from logbook import FileHandler
    # with FileHandler('app.log', level='DEBUG').push_application():
    #     log.info('Logging to file...')
    #     log.debug('This will go to app.log if level is DEBUG or lower.')

view raw JSON →