Rollbar Python SDK
Rollbar Python SDK (pyrollbar) provides easy and powerful exception tracking with Rollbar. It allows sending messages and exceptions with arbitrary context, enabling aggregation and quick debugging of production issues. The current stable version is 1.3.0, with active development including regular minor and patch releases, and support for newer Python versions.
Warnings
- breaking Support for Python 3.6 was removed in Rollbar Python SDK v1.3.0. Installations on Python 3.6 will fail or encounter issues.
- gotcha The `rollbar.init()` call must occur before any `rollbar.report_*` functions are called. Incorrect or missing `access_token` or `environment` parameters will prevent reports from being sent.
- gotcha Rollbar uses background threads to send reports asynchronously. If your application exits immediately after an error, reports may not be sent. For command-line scripts or short-lived processes, `rollbar.wait()` might be necessary before exit.
- gotcha Reports with very large payloads (e.g., huge local variables or deep call stacks) may be truncated due to size limits. The SDK has internal shortening logic (improved in v1.1.0-alpha).
- gotcha For popular web frameworks (Django, Flask, FastAPI, Pyramid), it's highly recommended to use the dedicated Rollbar integrations (e.g., middleware) rather than just `rollbar.init()`. These integrations handle context, request data, and proper error capturing within the framework's lifecycle.
Install
-
pip install rollbar
Imports
- init
import rollbar rollbar.init(...)
- report_exc_info
import rollbar rollbar.report_exc_info()
- report_message
import rollbar rollbar.report_message('Hello Rollbar!') - RollbarHandler
from rollbar.logger import RollbarHandler
Quickstart
import rollbar
import os
# Configure Rollbar
rollbar.init(
access_token=os.environ.get('ROLLBAR_ACCESS_TOKEN', 'YOUR_ROLLBAR_ACCESS_TOKEN'),
environment='development',
root=os.path.dirname(os.path.abspath(__file__))
)
def main():
try:
raise ValueError("This is a test error from Rollbar Python SDK!")
except Exception:
rollbar.report_exc_info()
print("Error reported to Rollbar.")
rollbar.report_message("Application started successfully.", level='info')
print("Message reported to Rollbar.")
# In a real application, ensure background threads have time to send data
# before exiting. For simple scripts, you might need rollbar.wait().
# rollbar.wait()
if __name__ == '__main__':
main()