Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Configuring log4j Loggers to Ignore Spring/Hibernate Logging

DZone's Guide to

Configuring log4j Loggers to Ignore Spring/Hibernate Logging

· Performance Zone ·
Free Resource

Sensu is an open source monitoring event pipeline. Try it today.

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>

Sensu: workflow automation for monitoring. Learn more—download the whitepaper.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}