Showing posts with label debug. Show all posts
Showing posts with label debug. Show all posts

Mar 16, 2009

Configure Logging in Spring With Log4J

Configuring logging for Spring is easy as long as you keep in mind that anything related to classpath is a total mess.

First, you want to obtain commons-logging and log4j libraries, if you haven't yet put them in server's lib folder or added to the web application.

Second, create a /WEB-INF/log4j.xml file with the following content:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "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="%-4r [%t] %-5p %c %x - %m%n"/>
</layout>
</appender>

<root>
<priority value ="error" />
<appender-ref ref="console" />
</root>

<category name="org.springframework" additivity="false">
<priority value="debug" />
<appender-ref ref="console" />
</category>

</log4j:configuration>


In short, this tells to throw whatever is of error level from all sources, and info- and debug-level messages from anything in org.springframework package in stdout.

Then, add the following section to your web.xml:

  <context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>


As pointed out here, logger Listener must be declared before ContextLoaderListener.

Now, this seems to be enought, however, at this point many people (judging by the amount of complaints on Spring forums) face the problem of nothing of the described to work.

This was also the case for me, and the problem was in commons-logging.jar, which was sitting in %CATALINA_HOME%/lib folder. Once I removed it out of there and added commons-logging library to my web application, the System.out was full of Spring's debug messages.

Mar 9, 2009

Enable Debug Level For SLF4J

To my shame, I'm absolutely new to logging in Java, and so far I've gone as far as “you need to add this to enable debug-level logging in your application”:

log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n


The exact problem was that despite having that configuration, I actually didn't see any debug printouts emitted by one of the libraries I was using, just the info ones. The reason was that library used SLF4J and was bound to the SLF4J-Simple logger.

Since I had no time to go ahead and read through the whole SLF4J manual, what I did was removing slf4j-simple.jar and including slf4j2log4j.jar instead.

And all the debug messages were there.