Basic configuration of Log4j in Spring using a .xml file

There are 2 ways to configure Log4j in Spring using:

  • a .properties file
  • a .xml file

The configuration with the. xml file allows to take advantage of some aspects that can not be configured with the .properties file that is therefore regarded as obsolete.
This article will explain how to configure Log4j using the .xml file and the configuration with .properties file was treated in a previous article.
The tools used for the development of this application are:

  • Java 1.6.0_22
  • Tomcat 6.0.20
  • Spring 3.0.4
  • Log4j 1.2.15
  • NetBeans 6.9
    1. Create a NetBeans project or in other IDE called HelloWorld (you can reuse the same project of a previous article)


    1. Add the following libraries in the directory /WEB-INF/lib:
      • commons-logging.jar
      • log4j-1.2.15.jar
      • org.springframework.aop-3.0.4.RELEASE.jar
      • org.springframework.asm-3.0.4.RELEASE.jar
      • org.springframework.aspects-3.0.4.RELEASE.jar
      • org.springframework.beans-3.0.4.RELEASE.jar
      • org.springframework.context-3.0.4.RELEASE.jar
      • org.springframework.core-3.0.4.RELEASE.jar
      • org.springframework.expression-3.0.4.RELEASE.jar
      • org.springframework.instrument-3.0.4.RELEASE.jar
      • org.springframework.instrument.tomcat-3.0.4.RELEASE.jar
      • org.springframework.jdbc-3.0.4.RELEASE.jar
      • org.springframework.jms-3.0.4.RELEASE.jar
      • org.springframework.orm-3.0.4.RELEASE.jar
      • org.springframework.oxm-3.0.4.RELEASE.jar
      • org.springframework.test-3.0.4.RELEASE.jar
      • org.springframework.transaction-3.0.4.RELEASE.jar
      • org.springframework.web-3.0.4.RELEASE.jar
      • org.springframework.web.portlet-3.0.4.RELEASE.jar
      • org.springframework.web.servlet-3.0.4.RELEASE.jar
      • org.springframework.web.struts-3.0.4.RELEASE.jar


    1. Create the file /WEB-INF/web.xml

      The specific section about LogjJ is contained in the tags context-param and listener.
      In the context-param tag you define the path to the .xml file to configure Log4j; if you use the standard name log4j.xml, the context-param tag would be unnecessary.


    1. Create the file /WEB-INF/HelloWorld-servlet.xml


    1. Create the file /WEB-INF/classes/log4j-helloworld.xml

      In this example I used the variable webapp.root but you can use others like catalina.home.


    1. Create the file index.jsp in the root of the application


    1. Create the directory /WEB-INF/views and the file /WEB-INF/views/helloWorld.jsp


    1. Create the file /WEB-INF/classes/it/helloworld/controller/


  1. Deploy and launch the application and verify the log in the console and in the file ${webapp.root}/WEB-INF/logs/helloworld.log

4 replies on “Basic configuration of Log4j in Spring using a .xml file”

complimenti per il tutorial. Veramente molto utile e chiaro! Volevo segnalare solo una cosetta che manda un po in confusione. Tra le prime righe si trova questa frase:
“In questo articolo spiegherò come configurare Log4j usando il file .xml mentre la configurazione con il file .xml è stata trattata in un precedente articolo.”

penso sia una piccola distrazione ma secondo me dovrebbe essere

“In questo articolo spiegherò come configurare Log4j usando il file .xml mentre la configurazione con il file .properties è stata trattata in un precedente articolo.”

🙂 solamente per essere pignoli ^^

buona giornata
thanks for share

SEVERE: Exception sending context destroyed event to listener instance of class org.springframework.web.util.Log4jConfigListener
java.lang.NoClassDefFoundError: org/apache/log4j/LogManager
at org.springframework.util.Log4jConfigurer.shutdownLogging(
at org.springframework.web.util.Log4jWebConfigurer.shutdownLogging(
at org.springframework.web.util.Log4jConfigListener.contextDestroyed(
at org.apache.catalina.core.StandardContext.listenerStop(
at org.apache.catalina.core.StandardContext$
at Source)
at org.apache.catalina.core.StandardContext.stopInternal(
at org.apache.catalina.util.LifecycleBase.stop(
at org.apache.catalina.util.LifecycleBase.start(
at org.apache.catalina.core.ContainerBase.startInternal(
at org.apache.catalina.core.StandardHost.startInternal(
at org.apache.catalina.util.LifecycleBase.start(
at org.apache.catalina.core.ContainerBase.startInternal(
at org.apache.catalina.core.StandardEngine.startInternal(
at org.apache.catalina.util.LifecycleBase.start(
at org.apache.catalina.core.StandardService.startInternal(
at org.apache.catalina.util.LifecycleBase.start(
at org.apache.catalina.core.StandardServer.startInternal(
at org.apache.catalina.util.LifecycleBase.start(
at org.apache.catalina.startup.Catalina.start(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(
at org.apache.catalina.startup.Bootstrap.main(
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.LogManager
at org.apache.catalina.loader.WebappClassLoader.loadClass(
at org.apache.catalina.loader.WebappClassLoader.loadClass(
… 26 more

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.