Programing/Java

Java 로그에서 StackTrace 가 생략되는 현상

Tomining 2016. 4. 4. 23:22
시스템을 운영하던 중 StackTrace 가 로그상에서 출력되지 않는 현상이 발견되었습니다.
Log4j 를 사용하고 있는 환경에서 아래와 같이 로그 출력시 Exception 객체를 넘겨주어 Trace 가 출력되도록 하였음에도 생략되었습니다.

log.error(“Error message”, e);

왜 이런 현상이 발생하는 것일까요?
구글링을 하던 중 아래 링크를 발견하였습니다.

JDK release note 를 확인해보면 아래와 같이 설명하고 있습니다.

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.

요약해 보면, 특정 Exception 이 몇번 반복해서 발생한다면 성능 이슈로 stack trace 를 제공하지 않는 Exception 로그를 compiler 는 선택한다라고 되어 있습니다.
만약 이런 Option 을 사용하지 않으려면 -XX:-OmitStackTraceInFastThrow 옵션을 설정해야 합니다.