holtc holtc - 3 months ago 31
Java Question

Log4j logging twice with different formats

I can't figure out why log4j is appending twice with different formats. Anyone come across this before?

Here is my log4j.xml file:

<log4j:configuration>

<appender name="async" class="org.apache.log4j.AsyncAppender">
<!-- this parameter need to be set to false to avoid application from hanging. -->
<param name="Blocking" value="false" />
<appender-ref ref="myAppender" />
</appender>

<appender name="myAppender" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{ISO8601} [%t] %c %x - %m%n" />
</layout>
</appender>

<logger name="com.server">
<level value="INFO" />
</logger>

<logger name="org.springframework">
<level value="INFO" />
</logger>

<logger name="org.hibernate.LazyInitializationException" additivity="false">
<level value="off" />
<appender-ref ref="async" />
</logger>

<logger name="net.sf.ehcache">
<level value="INFO" />
</logger>

<logger name="com.mchange">
<level value="INFO" />
</logger>

<root>
<priority value="INFO" />
<appender-ref ref="async" />
</root>

</log4j:configuration>


And here is some sample output:

INFO 2016-08-26 11:01:38,353 [main] com.server.Server - Server started successfully...
11:01:38,353 INFO : Server started successfully...


EDIT: When I change the "myAppender" appender threshold to "ERROR", the second log message shown (The one starting with the time, not "INFO") is still being generated. Is there some default logger that I need to disable? It seems that something is still logging those "INFO" level messages, even though the specified appender is for "ERROR" level messages. Also, if I comment out the entire log4j.xml file, the second log message (and all like it) are still being logged. How can I prevent this? Thanks!

Answer

Problem solved, I had to do:

<logger name="com.server" additivity="false">
    <level value="INFO" />
    <appender-ref ref="async" />
</logger>