Phoenix and Plug integration guide
Typical installation time: 5 minutes
Hi there! Youâve found Honeybadgerâs guide to Phoenix error tracking and performance monitoring. Once installed, Honeybadger will automatically report errors and performance insights from your Phoenix application.
Installing the package
Section titled âInstalling the packageâAdd the Honeybadger package to deps/0 in your applicationâs mix.exs:
defp deps do [{:honeybadger, "~> 0.30"}]endThen run:
mix do deps.get, deps.compileFinally, update your appâs config.exs file to include the Honeybadger
configuration:
config :honeybadger, app: :my_app, api_key: "PROJECT_API_KEY", environment_name: config_env(), insights_enabled: true # Enable logging and performance insightsSee the Configuration reference for additional info.
Testing your installation
Section titled âTesting your installationâTo test your installation, fire up iex -S mix, then run:
Honeybadger.notify("Hello Elixir!")If Honeybadger is configured correctly, you should see a new error report in your Honeybadger project dashboard.
You can also send a test error with the included Mix task. It sends the error
even in excluded environments, so you do not need to change exclude_envs:
mix honeybadger.testAfter youâve tested your Honeybadger installation, you may want to configure one or more of the following integrations to automatically report errors.
Enabling automatic error reporting
Section titled âEnabling automatic error reportingâThe Honeybadger package can be used as a Plug alongside your Phoenix applications, as a logger backend, and/or as a standalone client for sprinkling in exception notifications where they are needed.
The Honeybadger Plug adds a
Plug.ErrorHandler
to your pipeline. Simply use the Honeybadger.Plug module inside of a Plug or
Phoenix.Router and any crashes will be automatically reported to Honeybadger.
Itâs best to use Honeybadger.Plug after the Router plugs so that
exceptions due to non-matching routes are not reported to Honeybadger.
Phoenix example
Section titled âPhoenix exampleâdefmodule MyappWeb.Router do use MyappWeb, :router use Honeybadger.Plug
pipeline :browser do [...] endendPlug example
Section titled âPlug exampleâdefmodule MyPlugApp do use Plug.Router use Honeybadger.Plug
[... the rest of your plug ...]endConfiguring Elixirâs logger
Section titled âConfiguring Elixirâs loggerâWhen configured, Honeybadger will report errors for any
SASL-compliant
processes when they crash. Just set the use_logger option to true in your
applicationâs config.exs and youâre good to go:
config :honeybadger, use_logger: trueManually reporting errors
Section titled âManually reporting errorsâYou can use the Honeybadger.notify/2 function to manually report rescued
exceptions:
try do # Buggy code goes hererescue exception -> Honeybadger.notify(exception, stacktrace: __STACKTRACE__)endYou can also pass a string message to the notify function (the second argument
is optional):
Honeybadger.notify("Sign in failed", metadata: %{ user_id: current_user.id})See Reporting Errors for more information.
Reporting custom events
Section titled âReporting custom eventsâUse the Honeybadger.event/1 and Honeybadger.event/2 functions to send events
to Honeybadger Insights for logging and performance
monitoring:
Honeybadger.event(%{ event_type: "user_created", user: user.id})
# Honeybadger.event/2 is a shorthand that automatically adds the event_type# property to the event:Honeybadger.event("project_deleted", %{ project: project.name})See Sending custom events for more details.
Sending logs from your infrastructure
Section titled âSending logs from your infrastructureâHoneybadger isnât just for errors and application data! You can use our syslog, Vector, or PaaS integrations to send additional data from your infrastructure to Honeybadger Insights, where you can query, visualize, and analyze all of your production data in one place.
Version requirements
Section titled âVersion requirementsâSee Supported Versions.