Java integration guide

Add Bugsnag to your Java projects to automatically capture and report exceptions on production.

Installation

Using Gradle

Add bugsnag to the dependencies section in your build.gradle:

compile 'com.bugsnag:bugsnag:3.+'

Using Maven

Add bugsnag as a dependency in your pom.xml

<dependency>
  <groupId>com.bugsnag</groupId>
  <version>[3.0,4.0)</version>
  <artifactId>bugsnag</artifactId>
</dependency>

Then run mvn install to install the library.

Manual JAR installation

Download the latest release and place it in your app’s classpath.

The following dependencies are also required in your app’s classpath:

SLF4J

Bugsnag Java uses SLF4J for internal logging, which provides a facade to several popular logging libraries. If you do not specify which logging library you wish to use, a no-operation implementation which outputs a build warning will be used.

To suppress this build warning, you should add one of the SLF4J bindings as either a Gradle/Maven dependency in your project, or as a Jar file.

Basic configuration

Import the Bugsnag class in your code and create an instance to begin capturing exceptions:

import com.bugsnag.Bugsnag;

Bugsnag bugsnag = new Bugsnag("your-api-key-here");

Further configuration

If you’d like to configure Bugsnag further, check out the configuration options reference.

Reporting unhandled exceptions

Bugsnag attaches a Thread.UncaughtExceptionHandler, so after completing installation and basic configuration, unhandled exceptions will be automatically reported to your Bugsnag dashboard.

If you do not want Bugsnag to automatically report unhandled exceptions, set the sendUncaughtExceptions parameter to false when instantiating Bugsnag:

Bugsnag bugsnag = new Bugsnag("your-api-key-here", false);

Reporting handled exceptions

If you would like to send handled exceptions to Bugsnag, you can pass any Throwable object to the bugsnag.notify method:

try {
    // Some potentially crashy code
} catch (Throwable exception) {
    bugsnag.notify(exception);
}

For more information see reporting handled exceptions.

Sending diagnostic data

It can often be helpful to attach application-specific diagnostic data to exception reports. This can be accomplished as follows:

bugsnag.addCallback((report) -> {
    // Will appear as the 'name' in the 'subsystem' tab
    report.addToTab("subsystem", "name", "Your subsystem name");
});

Or without using the Java 8 lambda syntax:

import com.bugsnag.callbacks.Callback;
import com.bugsnag.Report;

bugsnag.addCallback(new Callback() {
    @Override
    public void beforeNotify(Report report) {
        // Will appear as the 'name' in the 'subsystem' tab
        report.addToTab("subsystem", "name", "Your subsystem name");
    }
});

For more information, see customizing error reports.

Identifying users

In order to correlate errors with customer reports, or to see a list of users who experienced each error, it is helpful to capture and display user information on your Bugsnag dashboard.

bugsnag.addCallback((report) -> {
    report.setUserName("User Name");
    report.setUserEmail("user@example.com");
    report.setUserId("12345");
});

Or without using the Java 8 lambda syntax:

import com.bugsnag.callbacks.Callback;
import com.bugsnag.Report;

bugsnag.addCallback(new Callback() {
    @Override
    public void beforeNotify(Report report) {
        report.setUserName("User Name");
        report.setUserEmail("user@example.com");
        report.setUserId("12345");
    }
});

For more information, see customizing error reports.

Java Servlet API

If you are using the javax.servlet API then Bugsnag will automatically populate details of the request such as the client IP address, request URL and parameters.

To report exceptions thrown by servlets you should implement your own error handler and call bugsnag.notify() from within it.

Session tracking

Bugsnag tracks the number of “sessions” that happen within your application. This allows you to compare stability scores between releases and helps you to understand the quality of your releases.

For plain Java applications, a session is captured and reported for each HTTP request which uses the Servlet API. This behaviour can be disabled using the setAutoCaptureSessions configuration option.

If you want full control over what is deemed a session, you can switch off automatic session tracking with the setAutoCaptureSessions option, and call startSession() directly.

Tracking releases

Configure your app version to see the release that each error was introduced in.

bugsnag.setAppVersion("1.0.0");

Then set up a build tool integration to enable linking to code in your source control provider from the releases dashboard, timeline annotations, and stack traces.