5

I am sure this is a simple fix but can't for the life of me figure out why. I am getting random NPEs and have log4j (2.0.2) logging the error, however despite having the %ex in my log4j configuration file, it prints no stack trace.

11-01-2019 02:39:33.212 [Thread-307] ERROR AlarmParse.ProcessAlarm: java.lang.NullPointerException -

Log4j2 configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration monitorInterval="30" status="INFO">
  <appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg - %ex%n"/>
    </Console>

    <File name="MyFile" fileName="G:/iMCAlarmParse/logs/AppLog.log">
        <PatternLayout pattern="%d{MM-dd-yyyy HH:mm:ss.SSS} [%t] %-5level %c{2}: %msg - %ex%n"/>
    </File>

Java:

try {
    //<Many sequential class calls>
} catch (Exception e) {
    log4j.error(e);
}
0

1 Answer 1

7

By log4j.error(e) you are actually invoking info(Object message) method which does not take extra care of Exception.

But, to print stack trace, you need to invoke one of the variants which handles Throwable (super class of Exception) separately. One of the easiest method for this could be info(CharSequence arg0, Throwable arg1).

E.g. you can simply change the line in the catch block to:

log4j.error("", e);
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome. I've recompiled and am deploying the JAR to the server. Now we wait.... :)
Perfect, thanks. The stack trace seems a bit short, but it identified the failing class.
Try %ex{full} for stack trace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.