Configuring log4j Loggers to Ignore Spring/Hibernate Logging
Join the DZone community and get the full member experience.
Join For FreeIf 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>
Published at DZone with permission of Faheem Sohail, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments