Saturday, January 25, 2014

Adding log4j to your Spring application

If you want to add log4j to your application, here's one way to do it:

First, add the log4j dependency. With gradle, it looks like this:

   compile 'log4j:log4j:1.2.16'

You can find the entries for maven, ivy etc. from this url:

http://mvnrepository.com/artifact/log4j/log4j/1.2.16

Then, you have to either add log4j.properties or log4j.xml to your src/resources folder.

For example, here's one created by the spring mvc project creation wizard in STS:

<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<logger name="com.kanjisoft.mvcapp">
<level value="info" />
</logger>
<logger name="org.springframework.core">
<level value="info" />
</logger>
<logger name="org.springframework.beans">
<level value="info" />
</logger>
<logger name="org.springframework.context">
<level value="info" />
</logger>

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

<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
</log4j:configuration>

Note how the spring packages are conveniently included. If you want to see them, simply change those ones to "debug". 

References: 
http://www.mkyong.com/spring-mvc/spring-mvc-log4j-integration-example/
http://mvnrepository.com/artifact/log4j/log4j/1.2.16xml version="1.0" encoding="UTF-8"?>



No comments:

Post a Comment