Even if you create ʻExceptionby yourself with a framework such as
SpringBoot` and throw an error as a known behavior, the stack trace will be displayed in the log as it is.
In other words, it's clear that the cause of the error is within the application, but it's simply displaying useless information.
If the stack trace is long, it will interfere with reading comprehension, and the log capacity will increase too much, which may cause problems, so it is better not to have extra information.
Therefore, filter the stack trace.
You can do this by preparing a filtering function as shown below and setStackTrace
in the constructor.
The package name is a string like com.wrongwrong.example
, which specifies the root package.
public class MyException extends RuntimeException {
static StackTraceElement[] filteringStackTrace(StackTraceElement[] elements) {
return Arrays.stream(elements)
.filter(it -> it.getClassName().startsWith("${package name}"))
.toArray(StackTraceElement[]::new);
}
public MyException(String msg) {
super(msg);
setStackTrace(filteringStackTrace(getStackTrace()));
}
}
Recommended Posts