Configuring log4j loggers to ignore spring/hibernate (and others) logging

Configuring log4j loggers to ignore spring/hibernate (and others) logging

If you use log4j with your project that also uses Spring framework and/or hibernate or any other framework, and you would feel the need to set different Log levels for your own code and for third party components. You may also want different log levels for your own components. The answer is simple. Configure different loggers for different package names and provide different log levels for each. For example, lets take a look at the following log4j XML


<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
   <appender name="CA">
      <layout>
         <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
      </layout>
   </appender>
   <appender name="FA">
      <param name="File" value="output.log" />
      <param name="Threshold" value="WARN" />
      <layout>
         <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
      </layout>
   </appender>
   <logger name="org.springframework">
      <level value="ERROR" />
   </logger>
   <root>
      <level value="INFO" />
      <appender-ref ref="CA" />
      <appender-ref ref="FA" />
   </root>
</log4j:configuration>

 

In the logger tag, specify the package name/prefix/regex and then specify the log level. The following logger will output only logs of level ERROR and above for packages starting with org.springframework

 


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