Python notifier for reporting exceptions, errors, and log messages to Rollbar.
Install using pip:
import rollbar rollbar.init('POST_SERVER_ITEM_ACCESS_TOKEN', 'production') # access_token, environment try: main_app_loop() except IOError: rollbar.report_message('Got an IOError in the main loop', 'warning') except: # catch-all rollbar.report_exc_info() # equivalent to rollbar.report_exc_info(sys.exc_info())
In your settings.py
, add 'rollbar.contrib.django.middleware.RollbarNotifierMiddleware'
as the last item in
MIDDLEWARE_CLASSES
in Django 1.9 and earlier:
MIDDLEWARE_CLASSES = [ # ... other middleware classes ... 'rollbar.contrib.django.middleware.RollbarNotifierMiddleware', ]
MIDDLEWARE
in Django 1.10 and up:
MIDDLEWARE = [ # ... other middleware classes ... 'rollbar.contrib.django.middleware.RollbarNotifierMiddleware', ]
Add these configuration variables in settings.py
:
ROLLBAR = { 'access_token': 'POST_SERVER_ITEM_ACCESS_TOKEN', 'environment': 'development' if DEBUG else 'production', 'branch': 'master', 'root': '/absolute/path/to/code/root', }
Be sure to replace POST_SERVER_ITEM_ACCESS_TOKEN
with your project's post_server_item
access token, which you can find in the Rollbar.com interface.
Check out the Django example.
In your ini
file (e.g. production.ini
), add rollbar.contrib.pyramid
to the end of your pyramid.includes
:
[app:main] pyramid.includes = pyramid_debugtoolbar rollbar.contrib.pyramid
And add these rollbar configuration variables:
[app:main] rollbar.access_token = POST_SERVER_ITEM_ACCESS_TOKEN rollbar.environment = production rollbar.branch = master rollbar.root = %(here)s
Be sure to replace POST_SERVER_ITEM_ACCESS_TOKEN
with your project's post_server_item
access token, which you can find in the Rollbar.com interface.
The above will configure Rollbar to catch and report all exceptions that occur inside your Pyramid app. However, in order to catch exceptions in middlewares or in Pyramid itself, you will also need to wrap your app inside a pipeline
with Rollbar as a filter
.
To do this, first change your ini
file to use a pipeline
. Change this:
To:
[pipeline:main] pipeline = rollbar YOUR_APP_NAME [app:YOUR_APP_NAME] pyramid.includes = pyramid_debugtoolbar rollbar.contrib.pyramid rollbar.access_token = POST_SERVER_ITEM_ACCESS_TOKEN rollbar.environment = production rollbar.branch = master rollbar.root = %(here)s [filter:rollbar] use = egg:rollbar#pyramid access_token = POST_SERVER_ITEM_ACCESS_TOKEN environment = production branch = master root = %(here)s
Note that the access_token, environment, and other Rollbar config params do need to be present in both the app
section and the filter
section.
Additionally, note that because Pyramid uses INI files for configuration, any changes to nested settings, like the locals
dictionary, will need to be handled in code.
Check out rollbar-flask-example.
Be sure to add the required blinker
dependency! See requirements.txt
in the example repo for how.
Import the plugin and install! Can be installed globally or on a per route basis.
import bottle from rollbar.contrib.bottle import RollbarBottleReporter rbr = RollbarBottleReporter(access_token='POST_SERVER_ITEM_ACCESS_TOKEN', environment='production') #setup rollbar bottle.install(rbr) #install globally @bottle.get('/') def raise_error(): ''' When navigating to /, we'll get a regular 500 page from bottle, as well as have the error below listed on Rollbar. ''' raise Exception('Hello, Rollbar!') if __name__ == '__main__': bottle.run(host='localhost', port=8080)
Be sure to replace POST_SERVER_ITEM_ACCESS_TOKEN
with your project's post_server_item
access token, which you can find in the Rollbar.com interface.
Check out the Twisted example.
For generic Python or a non-Django/non-Pyramid framework just initialize the Rollbar library with your access token and environment.
rollbar.init('POST_SERVER_ITEM_ACCESS_TOKEN', environment='production', **other_config_params)
Other options can be passed as keyword arguments. See the reference below for all options.
pyrollbar comes with a command-line tool that can be used with other UNIX utilities to create an ad-hoc monitoring solution.
e.g. Report all 5xx haproxy requests as warning
tail -f /var/log/haproxy.log | awk '{print $11,$0}' | grep '^5' | awk '{$1="";print "warning",$0}' | rollbar -t POST_SERVER_ITEM_ACCESS_TOKEN -e production -v
e.g. Test an access token
rollbar -t POST_SERVER_ITEM_ACCESS_TOKEN -e test debug testing access token
$ rollbar --help
Usage: rollbar [options]
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-t ACCESS_TOKEN, --access_token=ACCESS_TOKEN
You project's access token from rollbar.com.
-e ENVIRONMENT, --environment=ENVIRONMENT
The environment to report errors and messages to.
-u ENDPOINT_URL, --url=ENDPOINT_URL
The Rollbar API endpoint url to send data to.
-m HANDLER, --handler=HANDLER
The method in which to report errors.
-v, --verbose Print verbose output.
The Django, Pyramid, Flask, and Bottle integrations will automatically report uncaught exceptions to Rollbar.
To report a caught exception to Rollbar, use rollbar.report_exc_info()
:
try: do_something() except: rollbar.report_exc_info(sys.exc_info()) # or if you have a webob-like request object, pass that as well: # rollbar.report_exc_info(sys.exc_info(), request)
You can also send any other log messages you want, using rollbar.report_message()
:
try: do_something() except IOError: rollbar.report_message('Got an IOError while trying to do_something()', 'warning') # report_message() also accepts a request object: #rollbar.report_message('message here', 'warning', request)
Here's a full example, integrating into a simple Gevent app.
""" Sample Gevent application with Rollbar integration. """ import sys import logging from gevent.pywsgi import WSGIServer import rollbar import webob # configure logging so that rollbar's log messages will appear logging.basicConfig() def application(environ, start_response): request = webob.Request(environ) status = '200 OK' headers = [('Content-Type', 'text/html')] start_response(status, headers) yield '<p>Hello world</p>' # extra fields we'd like to send along to rollbar (optional) extra_data = {'datacenter': 'us1', 'app' : {'version': '1.1'}} try: # will raise a NameError about 'bar' not being defined foo = bar except: # report full exception info rollbar.report_exc_info(sys.exc_info(), request, extra_data=extra_data) # and/or, just send a string message with a level rollbar.report_message("Here's a message", 'info', request, extra_data=extra_data) yield '<p>Caught an exception</p>' # initialize rollbar with an access token and environment name rollbar.init('POST_SERVER_ITEM_ACCESS_TOKEN', 'development') # now start the wsgi server WSGIServer(('', 8000), application).serve_forever()
Default: master
Default: None
Default: True
Default: https://api.rollbar.com/api/1/item/
Valid levels: 'critical'
, 'error'
, 'warning'
, 'info'
, 'debug'
and 'ignored'
.
Use 'ignored'
if you want an Exception (sub)class to never be reported to Rollbar.
Any exceptions not found in this configuration setting will default to 'error'
.
Django settings.py
example (and Django default):
from django.http import Http404 ROLLBAR = { ... 'exception_level_filters': [ (Http404, 'warning') ] }
In a Pyramid ini
file, define each tuple as an individual whitespace delimited line, for example:
rollbar.exception_level_filters =
pyramid.exceptions.ConfigurationError critical
#...
One of:
Default: thread
Default: ['pw', 'passwd', 'password', 'secret', 'confirm_password', 'confirmPassword', 'password_confirmation', 'passwordConfirmation', 'access_token', 'accessToken', 'auth', 'authentication']
Default: 3
Default: True
[
('body', 'request', 'POST'),
('body', 'request', 'json')
]
If locals.enabled
is True
, extra keys are also automatically added:
[
('body', 'trace', 'frames', '*', 'code'),
('body', 'trace', 'frames', '*', 'args', '*'),
('body', 'trace', 'frames', '*', 'kwargs', '*'),
('body', 'trace', 'frames', '*', 'locals', '*')
]
Default: []
If you run into any issues, please email us at support@rollbar.com
You can also find us in IRC: #rollbar on chat.freenode.net
For bug reports, please open an issue on GitHub.
git checkout -b my-new-feature
).git commit -am 'Added some feature'
)git push origin my-new-feature
)Tests are in rollbar/test
. To run the tests: python setup.py test
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4