JBoss Log4j LoggingEvent getThrowableInformation never returns null
mclough Dec 13, 2016 12:16 PMI am implementing Microsoft Azure Application Insights logging following these instructions for Log4j 1.2:
https://docs.microsoft.com/en-gb/azure/application-insights/app-insights-java-trace-logs
I have created a module and added ApplicationInsights to my Wildfly 10 standalone.xml like this:
<custom-handler name="APPLICATION_INSIGHTS" class="com.microsoft.applicationinsights.log4j.v1_2.ApplicationInsightsAppender" module="com.microsoft.applicationinsights"/>
The purpose of Application Insights logging is to add log messages (as traces) or exceptions into the Application Insights monitoring application. However every log message appears as an exception instead of a trace.
This is because in the Application Insights Appender code, (available here: https://github.com/Microsoft/ApplicationInsights-Java) in the ApplicationInsightsLogEvent class is an isException method:
public boolean isException() {
return this.loggingEvent.getThrowableInformation() != null;
}
So the appender decides if the logging event is a message or exception depending on if the throwable information is null or not.
In the standard implementation of Log4j LoggingEvent, (https://github.com/apache/log4j/blob/v1_2-branch/src/main/java/org/apache/log4j/spi/LoggingEvent.java), the getThrowableInformation is implemented as:
public ThrowableInformation getThrowableInformation() {
return throwableInfo;
}
This returns null if the event is for a non-exception message.
JBoss Log4j LoggingEvent from https://github.com/jboss-logging/log4j-jboss-logmanager/blob/master/src/main/java/org/apache/log4j/spi/LoggingEvent.java is documented as "Our LoggingEvent class which is designed to look and act just like log4j's".
The getThrowableInformation is implemented as:
public ThrowableInformation getThrowableInformation() {
if (cachedThrowableInformation == null) {
cachedThrowableInformation = new
ThrowableInformation(logRecord.getThrown(), logger);
}
return cachedThrowableInformation;
}
This method never returns null, even if the event is a normal log message. This does not "look and act" like Log4j, and it causes Application Insights to see every message as an exception.
I think that getThrowableInformation should return null if the event is not an exception, i.e. if logRecord.getThrown is null.
How can this be resolved?
Thanks