is an that logs uncaught exceptions using SLF4J. is a Dropwizard bundle that installs on startup. When a thread is about to terminate due to an uncaught exception the Java Virtual Machine will query the thread for its UncaughtExceptionHandler using Thread.getUncaughtExceptionHandler() and will invoke the handler's uncaughtException method, passing the thread and the exception as arguments. Slf4jUncaughtExceptionHandler UncaughtExceptionHandler Slf4jUncaughtExceptionHandlerBundle Slf4jUncaughtExceptionHandler
When an uncaught exception is thrown, logs the exception at the WARN level. With logback configuration like this: The logs look like this: Slf4jUncaughtExceptionHandler <appender name="Console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%highlight(%-5level) %cyan(%date{HH:mm:ss.SSS, ${LOGGING_TIMEZONE}}) %gray(\(%file:%line\)) [%white(%thread)] %blue(%marker) {%magenta(%mdc)} %green(%logger): %message%n%rootException</pattern> </encoder> </appender> WARN 12:00:00.000 (Slf4jUncaughtExceptionHandler.java:46) [main] {exceptionClass=io.liftwizard.logging.slf4j.uncaught.exception.handler.Slf4jUncaughtExceptionHandlerTest.RootException, liftwizard.error.message=example root, liftwizard.error.kind=io.liftwizard.logging.slf4j.uncaught.exception.handler.Slf4jUncaughtExceptionHandlerTest.RootException, threadName=main, exceptionMessage=example root, liftwizard.error.thread=main} io.liftwizard.logging.slf4j.uncaught.exception.handler.Slf4jUncaughtExceptionHandler: Exception in thread "main" io.liftwizard.logging.slf4j.uncaught.exception.handler.Slf4jUncaughtExceptionHandlerTest$CauseException: example cause at io.liftwizard.logging.slf4j.uncaught.exception.handler.Slf4jUncaughtExceptionHandlerTest.testUncaughtException(Slf4jUncaughtExceptionHandlerTest.java:26) ~[test-classes/:na] ... 68 common frames omitted Wrapped by: io.liftwizard.logging.slf4j.uncaught.exception.handler.Slf4jUncaughtExceptionHandlerTest$RootException: example root at io.liftwizard.logging.slf4j.uncaught.exception.handler.Slf4jUncaughtExceptionHandlerTest.testUncaughtException(Slf4jUncaughtExceptionHandlerTest.java:27) ~[test-classes/:na]
To use the exception handler with Dropwizard, add to the list of registered bundles. And add the dependency: Slf4jUncaughtExceptionHandlerBundle @Override public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) { bootstrap.addBundle(new Slf4jUncaughtExceptionHandlerBundle()); } <dependency> <groupId>io.liftwizard</groupId> <artifactId>liftwizard-bundle-logging-uncaught-exception-handler</artifactId> </dependency>
To use without the bundle, create an instance and set it as the default uncaught exception handler. And add the dependency: Slf4jUncaughtExceptionHandler Thread.setDefaultUncaughtExceptionHandler(new Slf4jUncaughtExceptionHandler()); <dependency> <groupId>io.liftwizard</groupId> <artifactId>liftwizard-logging-uncaught-exception-handler</artifactId> </dependency>