Utilities to handle throwable objects and format stack trace elements in JVM
based languages (Java
, Scala
, Kotlin
, Groovy
, etc.)
SmartExceptionConverter
in logback
to generate shorter stack traces without a code change
throwableConverter
to format stack traces in logstash-logback-encoder
Binaries are available at Github and Maven Central.
Use Java API
to access all features of the library. See
Java API wiki page for the complete list of API methods.
Install smart-exception
by adding one of the following dependencies to your build.gradle
.
dependencies {
implementation 'com.arthenica:smart-exception-java9:0.2.1'
}
dependencies {
implementation 'com.arthenica:smart-exception-java:0.2.1'
}
Import Exceptions
class, which contains all utility methods, from the correct package.
import com.arthenica.smartexception.java9.Exceptions;
import com.arthenica.smartexception.java.Exceptions;
Use getStackTraceString
method to create a shorter stack trace. By default, getStackTraceString
will generate the
same long stack trace as a string for you. You need to define some rules for getStackTraceString
to use. Those rules
define how the exception stack trace is processed and shortened.
Exceptions.getStackTraceString(e);
root
packages.root
package is the entry point in an exception chain. If you define a root
package, then all stack trace elements
before that root
package will be discarded, and you will get a cleaner, compact stack trace. A root
package can be
defined in two different ways.
root
package.Exceptions.registerRootPackage("com.arthenica");
Exceptions.getStackTraceString(e);
root
package while getting the stack trace.Exceptions.getStackTraceString(e, "com.arthenica");
org.springframework.web.util.NestedServletException: Method not found.
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:183)
at com.arthenica.smartexception.java.SpringTest.putUserTest(SpringTest.java:100)
org.springframework.web.util.NestedServletException: Method not found.
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:183)
at com.arthenica.smartexception.java.SpringTest.putUserTest(SpringTest.java:100)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
... 35 more
Install smart-exception-logback
by adding the following dependency to your build.gradle
.
Note that smart-exception-logback
requires Java 9 or later
dependencies {
implementation 'com.arthenica:smart-exception-logback:0.2.1'
}
SmartExceptionConverter
in pattern layout<conversionRule conversionWord="smartEx" converterClass="com.arthenica.smartexception.logback.SmartExceptionConverter"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger - %msg %smartEx{'rootPackage=com.arthenica','printModuleName=false','ignorePackage=jdk.internal','printPackageInformation=true'}%n</pattern>
</encoder>
</appender>
throwableConverter
to format stack traces in logstash-logback-encoder
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<throwableConverter class="com.arthenica.smartexception.logback.SmartExceptionConverter">
<printModuleName>false</printModuleName>
<printPackageInformation>true</printPackageInformation>
<rootPackage>com.arthenica</rootPackage>
<groupPackage>org.springframework</groupPackage>
<ignorePackage>jdk.internal</ignorePackage>
</throwableConverter>
</encoder>
</appender>
SmartException Version | Release Date |
---|---|
0.2.1 | May 30, 2022 |
0.2.0 | Mar 13, 2022 |
0.1.1 | Nov 28, 2021 |
0.1.0 | Apr 12, 2020 |
Install Java 9 or later
Run
./gradlew clean build
See the build status from the table below.
branch | status |
---|---|
main | |
development |
A more detailed documentation is available at Wiki.
SmartException
source code is organised into five modules.
common
includes shared classes and interfacesjava
has Java 7/8 and Android specific implementationjava9
has the implementation for Java 9 or laterlogback
includes logback
converter implementationtest
includes test classes that use library jars published in mavenCentral()
This project is licensed under the BSD 3-Clause License
.
Feel free to submit issues or pull requests.