Add BugSnag to your Java Spring and Spring Boot projects to automatically capture and report exceptions on production.
New to BugSnag?Create an account
Looking for performance monitoring? See ourperformance guide
Addbugsnag-spring
to thedependencies
section in yourbuild.gradle
:
implementation'com.bugsnag:bugsnag-spring:3.+'
The latest available version ofbugsnag-spring
isv3.7.2
.
Addbugsnag-spring
as a dependency in yourpom.xml
<dependency><groupId>com.bugsnag</groupId><version>[3.0,4.0)</version><artifactId>bugsnag-spring</artifactId></dependency>
Then runmvn install
to install the library.
Download thelatest release and place it with it’s dependencies in your app’s classpath.
BugSnag Spring usesSLF4J 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 theSLF4J bindings as either a Gradle/Maven dependency in your project, or as a Jar file.
The minimum supported version of Spring Boot is1.4.0
.
If using Spring without Spring Boot the minimum supported version is4.2.0
.
The simplest way to configure BugSnag for Spring is to use Spring Annotations.
Create a SpringConfiguration
class which exposesBugsnag
as a Spring bean and imports the configuration classBugsnagSpringConfiguration
to begin capturing exceptions:
@Configuration@Import(BugsnagSpringConfiguration.class)publicclassBugsnagConfig{@BeanpublicBugsnagbugsnag(){returnnewBugsnag("your-api-key-here");}}
You can find your API key in your project’s settings (shortcut:g
s
) in the dashboard.
Inject theBugsnag
bean into your Spring managed bean to report handled exceptions:
@AutowiredprivateBugsnagbugsnag;
If you’re using XML configuration instead of Spring Annotations, you can configure aBugsnag
bean and aBugsnagSpringConfiguration
bean to begin capturing exceptions:
<context:annotation-config/><beanid="bugsnag"class="com.bugsnag.Bugsnag"><constructor-argname="apiKey"value="your-api-key-here"/></bean><beanid="bugsnagSpringConfiguration"class="com.bugsnag.BugsnagSpringConfiguration"/>
Add a setter for theBugsnag
bean into your managed bean:
privateBugsnagbugsnag;publicvoidsetBugsnag(Bugsnagbugsnag){this.bugsnag=bugsnag;}
InjectBugsnag
into your bean:
<beanid="example"class="com.example.ExampleBean"><propertyname="bugsnag"ref="bugsnag"/></bean>
If you uselogback you can configure a BugSnag log appender to automatically report logged throwables. Seeconfiguring logback for additional setup instructions.
If you’d like to configure BugSnag further, check out theconfiguration options reference.
BugSnag attaches aThread.UncaughtExceptionHandler
, so after completinginstallation andbasic configuration, unhandled exceptions will be automatically reported to your BugSnag dashboard.
If you do not want BugSnag to automatically report unhandled exceptions, set thesendUncaughtExceptions
parameter tofalse
when instantiatingBugsnag
:
Bugsnagbugsnag=newBugsnag("your-api-key-here",false);
In addition to exceptions reported fromThread.UncaughtExceptionHandler
, unhandled exceptions are automatically reported to BugSnag if you are using:
Unhandled exceptions thrown when theDispatcherServletprocesses MVC and REST API requests are automatically reported to BugSnag.
Exceptions thrown from@Controller and @RestControllermethods will have a default severity ofERROR
.
Exceptions that are automatically assigned an HTTP response status by Spring(seeDefaultHandlerExceptionResolver)will have a default severity ofINFO
for4XX
responses (bad request) andERROR
for500
responses.
Uncaught exceptions thrown in@Scheduled
annotated methods will be automatically reported to BugSnag.
In order to automatically report uncaught exceptions thrown in@Async
methods the following configuration needs to be added to your application:
@ConfigurationpublicclassBugsnagAsyncConfigextendsAsyncConfigurerSupport{@AutowiredprivateBugsnagbugsnag;@OverridepublicAsyncUncaughtExceptionHandlergetAsyncUncaughtExceptionHandler(){returnnewBugsnagAsyncExceptionHandler(bugsnag);}}
If your method returns aFuture
, you will need to wrap the call tofuture.get
with a try-catch and notify bugsnag of the error:
Future<Data>future=restService.retrieveData();try{Datadata=future.get();}catch(Exceptionexc){bugsnag.notify(exc);}
If you would like to send handled exceptions to BugSnag, you can pass anyThrowable
object to thebugsnag.notify
method:
try{// Some potentially crashy code}catch(Throwableexception){bugsnag.notify(exception);}
For more information seereporting handled exceptions.
If you configure the logback appender exceptions will be sent to BugSnag when you include theThrowable
parameter in aLogger
call:
privatestaticfinalLoggerlogger=Logger.getLogger(MyClass.class);try{// Some potentially crashy code}catch(Throwableexception){logger.warn("Something went wrong here",exception);}
For configuration instructions seeconfiguring logback.
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' tabreport.addToTab("subsystem","name","Your subsystem name");});
For more information, seecustomizing error reports.
In order to correlate errors with customer reports, or to see a list of userswho experienced each error, it is helpful to capture and display userinformation on your BugSnag dashboard.
bugsnag.addCallback((report)->{report.setUser("User ID","user@example.com","User Name");});
For more information, seecustomizing error reports.
BugSnag will automatically populate details of requests such as the client IP address and request URL for handled and unhandled exceptions.
Check outthe full list of captured data for more details.
BugSnag tracks the number of “sessions” that happen within your application.This allows you tocompare stability scores between releases and helps you to understand the quality of your releases.
For Spring applications, a session is captured and reported for each HTTP request which uses Spring MVC. This behaviour can be disabled using thesetAutoCaptureSessions
configuration option.
If you want full control over what is deemed a session, you can switch off automatic session tracking with thesetAutoCaptureSessions
option, and callstartSession()
directly.
Configure yourapp version to see the release that each error was introduced in.
bugsnag.setAppVersion("1.0.0");
Then set up abuild tool integration to enable linking to code in your source control provider from the releases dashboard, timeline annotations, and stack traces.
bugsnag-spring
, the library powering this integration, on GitHub