Reporting errors
Use honeybadger.notify() to send errors to Honeybadger:
from honeybadger import honeybadger
try: # Buggy code goes hereexcept Exception as exception: honeybadger.notify(exception)Reporting errors without an exception
Section titled “Reporting errors without an exception”You can report any type of error to Honeybadger, not just exceptions. The
simplest form is calling honeybadger.notify with a custom class and message:
from honeybadger import honeybadger
honeybadger.notify( error_class='ValueError', error_message='Something bad happened!')
# Or send a simple string messagehoneybadger.notify("Something went wrong!")Passing additional options to honeybadger.notify
Section titled “Passing additional options to honeybadger.notify”In some cases you will want to override the defaults or add additional
information to your error reports. To do so, you can pass more arguments to
honeybadger.notify.
For example, you could add tags to a notification:
from honeybadger import honeybadger
honeybadger.notify(exception, tags=["my_custom_tag", "another_tag"])These are all the available arguments you can pass to honeybadger.notify:
| Parameter | Type | Required | Description |
|---|---|---|---|
exception | Exception | * | An instance of an exception |
error_class | str | ** | A string representation of a class name |
error_message | str | ** | An error message |
fingerprint | str | No | A unique identifier used to group related errors together |
context | dict | No | A dictionary containing additional context information |
tags | list[str] | No | A list of tags to associate with the error |
* Either provide an exception object, OR both error_class and
error_message.
** Required when not providing an exception object.
The honeybadger.notify() method returns a UUID string that uniquely identifies
the error notification.
Reporting errors from Python’s logging module
Section titled “Reporting errors from Python’s logging module”Honeybadger includes a log handler, HoneybadgerHandler, that reports log
records to Honeybadger. Each record is reported as an error with the class
"<LEVEL> Log" (for example, "ERROR Log") and the log message as the error
message. Any extra values and the record’s remaining attributes (such as
name, levelname, and lineno) are sent as context. Some standard attributes,
such as pathname, process, and thread, are left out.
The handler uses its own Honeybadger client, configured only with the api_key
you pass it and any HONEYBADGER_* environment variables. Options set with
honeybadger.configure() (or Django’s HONEYBADGER setting), such as
environment and before_notify, don’t apply to it.
import loggingfrom honeybadger.contrib.logger import HoneybadgerHandler
hb_handler = HoneybadgerHandler(api_key='PROJECT_API_KEY')hb_handler.setLevel(logging.ERROR)
logger = logging.getLogger(__name__)logger.addHandler(hb_handler)
logger.error("Something went wrong", extra={"user_id": 123})You can also add the handler with dictConfig (for example, in Django’s
LOGGING setting). Replace myapp with your app’s package name, and add an
entry for each package you want to report from:
LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'honeybadger': { 'level': 'ERROR', 'class': 'honeybadger.contrib.logger.HoneybadgerHandler', 'api_key': 'PROJECT_API_KEY', }, }, 'loggers': { 'myapp': { 'handlers': ['honeybadger'], }, },}