Python ErrorNot Notifier
ErrorNot is an errors agregator that let you track your errors without overflooding your inbox.
The Python ErrorNot notifier comes in two flavors:
- a WSGI application,
- a logging handler.
However you decide to use Python ErrorNot notifier, you should set some variables first:
from errornot import notifier notifier.API_KEY = "900b7e06850c9864f908469c" notifier.HOST = "localhost:3000" notifier.API_VERSION = "0.1.0"
The WSGI ErrorNot notifier
You can use the ErrorNot as a WSGI application. You just have to insert it somewhere in your applications stack (as low as possible, if you want to catch all your exceptions). The WSGI notifier will send some info about the request and the environment to ErrorNot. This is recommended way of using ErrorNot notifier in a WSGI web app.
Exemple of a very simple "Hello World" application:
def hello_world(environ, start_response): """Very simple WSGI app saying hello. """ start_response('200 OK', [('Content-Type', 'text/html')]) return ['''<title>Hello %(subject)s</title> <p>Hello World!</p>'''] def not_found(environ, start_response): """Called if no URL matches.""" start_response('404 NOT FOUND', [('Content-Type', 'text/plain')]) return ['Not Found'] def routing(environ, start_response): path = environ.get('PATH_INFO', '').lstrip('/') if path == "hello": return hello_world(environ, start_response) if path == "error": raise AssertionError("error msg") return not_found(environ, start_response) from wsgiref.simple_server import make_server from errornot import wsgi_notifier errornot_notifier = wsgi_notifier.WSGINotifier(routing) srv = make_server('localhost', 8080, errornot_notifier) srv.serve_forever()
The above application will raise an error if you visit http://localhost:8080/error, and post an error msg to the ErrorNot server you have configured first.
The logging handler
The ErrorNot notifier also has a logger handler, because it might be usefull to use it in a non WSGI application, or to manually trigger errors posting. If you are new to logging facilities in Python, please read http:docs.python.org/library/logging.html.
To use it that way, just bing the handler to one of your loggers, and start logging exceptions. ErrorNot being intended to log errors, the notifier will look in the stack for exceptions and will grab informations about the last raised. As so, it is recommended to use inside except blocks only.
import logging logging.basicConfig() logger = logging.getLogger('test') logger.setLevel(logging.ERROR) logger.addHandler(notifier.ErrorNotHandler()) # This will post an error: try: raise AssertionError("Evil error") except Exception, e: logger.exception(e)
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see http:www.fsf.org/licensing/licenses/agpl-3.0.html.