May 21

If you do not want to store your log4j configuration in your classes directory then you need to tell log4j where it can find it.

One possibility is to configure it in your applicationContext.xml as shown here

<bean id="log4jInitialization"
 class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
   <property name="targetClass"
      value="org.springframework.util.Log4jConfigurer" />
   <property name="targetMethod" value="initLogging" />
   <property name="arguments">
      <list>
         <value>conf/log4j.xml</value>
      </list>
   </property>
</bean>

If you are running your web application as an expanded war then another option is to use a listener in your web.xml. This will only work on an unexpanded war.

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

Post to Twitter Post to Digg

written by objects \\ tags:


2 Responses to “How to specify log4j configuration in spring application?”

  1. 1. neel Says:

    While using the second approach I am getting the below exception.

    <Unable to set th
    e activation state to true for the application SAM.
    weblogic.management.ApplicationException: start() failed.
    {
    Module Name: SAM, Error: weblogic.management.DeploymentException: Cannot set web
    app root system property when WAR file is not expanded – with nested exception:

    [java.lang.IllegalStateException: Cannot set web app root system property when W
    AR file is not expanded]
    }
    at weblogic.j2ee.J2EEApplicationContainer.start(J2EEApplicationContainer
    .java:2134)
    at weblogic.j2ee.J2EEApplicationContainer.activate(J2EEApplicationContai
    ner.java:2168)
    at weblogic.j2ee.J2EEApplicationContainer.activate(J2EEApplicationContai
    ner.java:2115)
    at weblogic.management.deploy.slave.SlaveDeployer$Application.setActivat
    ion(SlaveDeployer.java:3082)
    at weblogic.management.deploy.slave.SlaveDeployer.setActivationStateForA
    llApplications(SlaveDeployer.java:1751)
    at weblogic.management.deploy.slave.SlaveDeployer.resume(SlaveDeployer.j
    ava:359)
    at weblogic.management.deploy.DeploymentManagerServerLifeCycleImpl.resum
    e(DeploymentManagerServerLifeCycleImpl.java:229)
    at weblogic.t3.srvr.SubsystemManager.resume(SubsystemManager.java:131)
    at weblogic.t3.srvr.T3Srvr.resume(T3Srvr.java:966)
    at weblogic.t3.srvr.T3Srvr.run(T3Srvr.java:361)
    at weblogic.Server.main(Server.java:32)

    Pls suggest

  2. 2. objects Says:

    That method will not work on an unexpanded war.
    Have updated the post.

Leave a Reply