0
try {
   // some code
}
catch (Exception e) {
   Logger.log(Level.WARN, "Unable to complete the job. ID: " + id, e);
}

So, obviously a developer expects if something goes wrong, it would log the exception (exception type & stacktrace)

Here is the log print which I got

[27 May 2019 13:30:07][http-nio-8080-exec-13][WARN]: Unable to complete the job. ID: 123457890

Here is the Log4j config

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=debug.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d{dd MMM yyyy HH:mm:ss}][%t][%p]: %m%n

I know ways to get the exception details, I want to understand this behavior. How does this happen? Does the logger ignore the passed on parameters?

Adhi
  • 149
  • 2
  • 11
  • Try `Level.ERROR`, because WARN might not automatically log the exception stacktrace etc. – Jens May 27 '19 at 11:30
  • try Logger.error() for exceptions. – AmitD May 27 '19 at 11:30
  • 2
    @GamingFelix Do you know how log4j works? Check out https://logging.apache.org/log4j/2.0/log4j-api/apidocs/org/apache/logging/log4j/Logger.html#log-org.apache.logging.log4j.Level-java.lang.CharSequence-java.lang.Throwable- It is supposed to print out the stacktrace if it is in your logging format, if you pass an exception as the last argument. You should never call e.stacktrace yourself if you are using a logging library - the logging library decides whether to show the stacktrace or not. – Erwin Bolwidt May 27 '19 at 11:47
  • Check out the log format / pattern, make sure that you are not suppressing the stacktrace. See: https://stackoverflow.com/questions/37206067/log4j-not-printing-complete-stack-trace – Erwin Bolwidt May 27 '19 at 11:50
  • Ah, yeah I know how it works. I didn't see it was log4j, I've had this issue myself before. You're right that it's about what parameter the exception is when you send it in :) That's probably his issue. Maybe it's not actually meant to be the last argument. But I really didn't see it saying it was log4j anywhere – GamingFelix May 27 '19 at 11:50
  • @ErwinBolwidt You should write it as an answer. Most likely that is the issue. I know in my project the format/pattern was so that exception came as first argument. – GamingFelix May 27 '19 at 11:55
  • @ErwinBolwidt Thanks for your suggestion. The stacktrace is not suppressed here – Adhi May 27 '19 at 12:01
  • Can you show what your pattern is? And what do you get when you print the stacktrace? – GamingFelix May 28 '19 at 10:50

2 Answers2

1

ERROR level is used to log the stacktrace of an error in log file.

Try using.

    try {
        // some code
    } catch (Exception e) {
       Logger.log(Level.ERROR, "Unable to complete the job. ID: " + id, e);
    }

if it still don't work, please share your logging library. Will check with that.

AmitD
  • 94
  • 6
0

My only guess would be that this happens due to hotspot optimization:

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.

This was borrowed from this answer to a related question:

log4j not printing the stacktrace for exceptions

Matthew
  • 1,905
  • 3
  • 19
  • 26
  • I am aware this optimization. The problem here is it does not even print the exception type (class name like NPE) – Adhi May 27 '19 at 11:45