3

I inherited a bunch of code and I noticed that in the tomcat logs it says

log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

The faq link mentions that this occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration.

How can I figure out how exactly to fix this. The xml file has the following. So I'm guessing its because there is no log4j.xml and instead there is one for each environment. Assuming that is the problem how do I configure things correctly.

<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>classpath:log4j-${environment}.properties</value>
          </list>
       </property>
    </bean>
user782220
  • 10,677
  • 21
  • 72
  • 135
  • Find out what the value of `environment` resolves to (probably something like `dev` or `prod`, try doing a text search for "environment" in all files under tomcat, or by looking at the `ps` info) and then put a valid log4j config at `log4j-dev.xml` (or other name, depending on what environment actually resolves to) in your `WEB-INF/classes` directory of your deployed webapp. – heenenee Aug 09 '16 at 05:19
  • 3
    Does the property `${environment}` exists? Can you make a [mcve] and post your Spring config? – Tunaki Aug 09 '16 at 09:46

1 Answers1

0

Few collective solutions -

  1. While defining the bean, there shall be some property file from where the value(assume env) of ${environment} is being fetched. You must note that appending that value to the file name. Should exist under your project resources. In your case a file named log4j-env.properties.

  2. Would recommend ways of using different method of logging for different environments while categorising them under different directory, so as to modify your values instead as -

    <property name="arguments">
        <list>
            <value>#{environment + '/log4j.properties'}</value>
        </list>
    </property>
    
  3. You could configure your Log4j listener in the web.xml instead of the spring-context.xml, so it is up before Spring starts. as -

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/${environment}/log4j.properties</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    

Source - Initializing Log4J with Spring?

Note - On how to assign the correct value to environment at runtime, follow How to replace a value in web.xml with a Maven property?

Community
  • 1
  • 1
Naman
  • 27,789
  • 26
  • 218
  • 353