{"id":1540,"library":"logbook","title":"Logbook","description":"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.","status":"active","version":"1.9.2","language":"en","source_language":"en","source_url":"https://github.com/logbook/logbook","tags":["logging","observability","log management"],"install":[{"cmd":"pip install logbook","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"Logger","correct":"from logbook import Logger"},{"symbol":"StreamHandler","correct":"from logbook import StreamHandler"},{"symbol":"FileHandler","correct":"from logbook import FileHandler"},{"symbol":"TimedRotatingFileHandler","correct":"from logbook.more import TimedRotatingFileHandler"},{"symbol":"INFO","correct":"from logbook import INFO"},{"symbol":"DEBUG","correct":"from logbook import DEBUG"}],"quickstart":{"code":"from logbook import Logger, StreamHandler\nimport sys\n\nlog = Logger('MyApp')\n\ndef run_app():\n    log.info('Application started.')\n    log.debug('This is a debug message.')\n    try:\n        1 / 0\n    except ZeroDivisionError:\n        log.exception('An error occurred during calculation!')\n    log.info('Application finished.')\n\nif __name__ == '__main__':\n    # Handlers should be pushed/popped or used as context managers\n    with StreamHandler(sys.stdout, level='INFO').push_application():\n        run_app()\n\n    # Example with a file handler (ensure 'app.log' is writable)\n    # from logbook import FileHandler\n    # with FileHandler('app.log', level='DEBUG').push_application():\n    #     log.info('Logging to file...')\n    #     log.debug('This will go to app.log if level is DEBUG or lower.')\n","lang":"python","description":"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."},"warnings":[{"fix":"Ensure your project uses Python 3.9 or newer. For older Python environments, consider using Logbook 0.12.x or migrating to Python 3.","message":"Logbook versions 1.x are exclusively for Python 3 (specifically >=3.9 for 1.9.x). Attempting to use a modern Logbook version with Python 2.x or older Python 3 versions will result in `SyntaxError` or other compatibility issues.","severity":"breaking","affected_versions":"<1.0 and >=1.0"},{"fix":"Always use `with` statements for handlers: `with SomeHandler().push_application():`. For application-wide handlers, ensure `push_application()` is called early and `pop_application()` is called on shutdown, or rely on context managers where possible.","message":"Handlers in Logbook require explicit pushing and popping from the handler stack or usage as a context manager (`with handler.push_application():`). Forgetting to push a handler will result in no log output, and forgetting to pop can lead to resource leaks or unexpected behavior.","severity":"gotcha","affected_versions":"All versions"},{"fix":"After forking, ensure that file handlers in the child process are explicitly re-opened or re-initialized. The documentation often suggests using `logbook.ThreadsafeFileHandler` and explicitly closing/re-opening in child processes, or using inter-process communication for logging.","message":"When forking processes, especially with file-based handlers (`FileHandler`, `TimedRotatingFileHandler`), the file descriptors might not be correctly inherited or managed in the child process. This can lead to logs from child processes not being written or corrupting files.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}