{"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":"python","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":"Verify your Logbook installation and ensure `push_application()` returns the handler object when used as a context manager. 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":"When using handlers with `with handler.push_application():`, `push_application()` is expected to return the handler itself to support the context manager protocol. If `push_application()` unexpectedly returns `None` (e.g., due to an incorrect Logbook version or setup), it will result in a `TypeError: 'NoneType' object does not support the context manager protocol`, causing application crash and preventing any logging. Ensure handlers are correctly pushed/popped or that `push_application()` functions as expected for context management.","severity":"breaking","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,"search_vec":"'1.9.2':47 'base':52 'clean':35 'consid':21 'contextu':36 'contribut':54 'current':43 'design':7 'focus':33 'fresh':14 'handler':40 'intermitt':51 'intuit':23 'librari':6 'log':10,17,30,37,57,59 'logbook':1,2 'mainten':56 'manag':41,60 'modul':31 'observ':58 'occur':50 'offer':12 'often':20 'power':25 'python':5,27 'releas':49 'replac':11 'robust':39 'stabl':44 'standard':29 'take':15 'version':45","created_at":"2026-04-09T03:52:30.952157+00:00","updated_at":"2026-04-16T16:21:10.762208+00:00","problems":[{"fix":"Install the 'logbook' package using pip: `pip install logbook`","cause":"The 'logbook' library is not installed in the Python environment, or the Python interpreter cannot find it in its `sys.path`.","error":"ModuleNotFoundError: No module named 'logbook'"},{"fix":"Ensure that the `Logger` class is imported correctly with a capital 'L': `from logbook import Logger`","cause":"The `Logger` class is being incorrectly imported or referenced with a lowercase 'l' (logger) instead of an uppercase 'L' (Logger), or a different attribute is being accessed that does not exist directly on the `logbook` module.","error":"AttributeError: module 'logbook' has no attribute 'Logger'"},{"fix":"Pin the `logbook` version to one compatible with the dependent library (e.g., `pytest-logbook`). A common fix involves downgrading `logbook`: `pip install 'logbook<1.9'` or `pip install 'logbook==1.5.3'` (or a version known to be compatible with your other dependencies).","cause":"This error often occurs due to a version mismatch between `logbook` and other libraries that depend on its `compat` module (e.g., `pytest-logbook`), especially when newer versions of `logbook` have removed or refactored this internal module.","error":"AttributeError: module 'logbook' has no attribute 'compat'"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.10.1","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/getlogbook/logbook","docs":"https://logbook.readthedocs.io","changelog":null,"pypi":"https://pypi.org/project/logbook/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["observability","serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":"verified"}}