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 with it’s dependencies 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:

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

Logback configuration

If you are using logback you can configure a Bugsnag log appender to automatically report logged exceptions. See configuring logback for additional setup instructions.

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.

Using the logback appender

If you configure the logback appender exceptions will be sent to Bugsnag when you include the Throwable parameter in a Logger call:

private static final Logger logger = Logger.getLogger(MyClass.class);

try {
    // Some potentially crashy code
} catch (Throwable exception) {
    logger.warn("Something went wrong here", exception);
}

For configuration instructions see configuring logback.

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");
});

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.setUser("User ID", "user@example.com", "User Name");
});

For more information, see customizing error reports.

Java Servlet API

Bugsnag will automatically populate details of requests such as the client IP address and request URL for handled and unhandled exceptions.

Check out the full list of captured data for more details.

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.