admin管理员组

文章数量:1400744

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;
    }
  1. I debugged this method and found that it does not load classes in the exception's stack.
  2. I find this website: . It seems this issue had no progress.(I'm not sure)

本文标签: javaWill log4j2 still trigger class loading when printing the thread stackStack Overflow