首页 > 编程学习 > 记一次日志打印异常堆栈缺失

记一次日志打印异常堆栈缺失

发布时间:2023/4/8 7:24:48

记一次日志打印异常堆栈缺失

现象

线上监控告警收到一条告警日志信息,如下图,但是只有异常信息,查了很久也查不到打印的异常堆栈信息,

接口异常信息
java.lang.NullPointerException

查询代码,日志打印使用的logback ,本地启动,手动抛出异常,发现本地是会打印堆栈信息的

原因

查了资料,这里jvm在同一个方法抛出相同异常堆栈次数达到一定程度的时候,会使用JIT即时编译重新编译改方法,抛出一个不带堆栈的异常,所以没有打印堆栈信息,下面这些设定的异常才会

  • NullPointerException
  • ArithmeticException
  • ArrayIndexOutOfBoundsException
  • ArrayStoreException
  • ClassCastException

JIT即时编译

JIT is a part of the JVM that optimizes the performance of the application. JIT stands for Java-In-Time Compiler. The JIT compilation is also known as dynamic compilation. In this section, we will learn what is JIT in Java, its working, and the phases of the JIT compiler. What is JIT in Java? JIT in Java is an integral part of the JVM

简单说就是,将代码直接编译为机器指令代码,提高执行速度,

我们都知道java代码是险编译为class字节码文件,然后再通过jvm解释,生成机器指令的方式执行,而JIT就是跳过了jvm解释这一层

解决

  • 这段是官方说明,我们可以通过指定 -XX:-OmitStackTraceInFastThrow 参数来禁用此优化
The compiler in the server VM now provides correct stack backtraces for all "cold" built-in exceptions. For performance purposes, when such an exception is thrown a few times, the method may be recompiled. After recompilation, the compiler may choose a faster tactic using preallocated exceptions that do not provide a stack trace. To disable completely the use of preallocated exceptions, use this new flag: -XX:-OmitStackTraceInFastThrow
  • 已经出现的怎么找到异常堆栈?

    可以直接追溯此方法异常的最早出现的日志,里面是有堆栈信息打印的

Copyright © 2010-2022 mfbz.cn 版权所有 |关于我们| 联系方式|豫ICP备15888888号