Configuring Log4J for Spring in RAD/Eclipse
Configuring Log4J for a Spring project turned out to be quite a pain in RAD7. I could finally get them all working by putting some effort. The issue seemed to be the way commons-logging is tied up with Spring and how it prevents Log4J from performing it’s tasks. Here are the steps that would enable Log4J log spring information.
Before performing any of these steps, stop the server.
1. Create a file named commons-logging with a .properties extension and have the following 2 lines copied into it and save the file.
1 2 | priority=1 org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.Log4jFactory |
2. Copy this commons-logging.properties file into the lib directory of the runtime server:
Ex: C:\Program Files\IBM\SDP\runtimes\base_v6\lib
3. Put the following entries in web.xml
NOTE: Make sure that these entries are put ahead of any spring related entries.
1 2 3 4 5 6 7 8 9 10 | <!-- location of log4j config file --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/log4j.xml</param-value> </context-param> <!-- applies log4j configuration --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> |
4. Create a file named log4j.xml and copy the following contents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?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> <appender name="FILE_APPENDER" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="c:/spring.log" /> <param name="Append" value="true" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n" /> </layout> </appender> <logger name="org.springframework" additivity="false"> <priority value="debug" /> <appender-ref ref="console" /> <appender-ref ref="FILE_APPENDER" /> </logger> <root> <priority value="error" /> <appender-ref ref="console" /> </root> </log4j:configuration> |
5. Copy this log4j.xml file into the WEB-INF directory of your web application.
6. Ensure that you have removed the commons-logging jar from the Java EE Module Dependencies and add the log4j.jar on your Web project.
7. Do a clean build of the projects.
Start the server and you should see the spring related log informtion on the console and in the spring.log file. Easy ain’t it, but only if you know it.hehe 😉
Renegado369
I’m using WAS 7.
The server can’t start because it doesn’t find the class org.apache.commons.logging.impl.Log4jFactory and I get a ClassNotFound Exception. I tried put the commons-logging.jar into the same dir than properties file but didn’t work.
Where this jar have to been ?
Thank you in advanced
Rushi
Make sure you have the log4j jar in your webapp classpath. Open the EAR deployment descriptor and click on the deployment tab at the bottom. Then scroll down to the Applications section at the bottom of the page and change the classloader mode from PARENT_FIRST to PARENT_LAST.