I find these codes in our old project. It appears to filter the classes generated by reflections in the exception stack before printing the log. But I debugged this method and found that it does not load classes in the exception's stack.(Version: Java21 and Log4j2 2.17.1) So is these codes necessary to prevent the thread block which caused by class loading.
log.error("query failed", ExceptionUtil.filterReflectTrace(e));
/**
* Filtering reflection related classes, after JVM reflection optimization, log4j output exception stack will block threads due to repeated loading of these classes, especially GeneratedMethodAccessor
* @param e
* @return
*/
public static Throwable filterReflectTrace(Throwable e) {
if (!(e instanceof InvocationTargetException)) {
return e;
}
Throwable cause = e.getCause() == null ? e : e.getCause();
StackTraceElement[] traces = cause.getStackTrace();
List<StackTraceElement> list = new ArrayList<>();
for (StackTraceElement element : traces) {
String className = element.getClassName();
if (className.contains("GeneratedMethodAccessor") || className.contains("DelegatingMethodAccessorImpl")
|| className.contains("NativeMethodAccessorImpl")) {
continue;
}
list.add(element);
}
StackTraceElement[] newTraces = new StackTraceElement[list.size()];
cause.setStackTrace(list.toArray(newTraces));
return cause;
}
- I debugged this method and found that it does not load classes in the exception's stack.
- I find this website: . It seems this issue had no progress.(I'm not sure)