1

In my Spring 4.1 based application, I am not using any web.xml. How can I initialize log4j without web.xml in my app? I came across this SO question which suggests the required configuration through web.xml, but I am not sure how to do it through code.

. I have placed my log4j.properties in WEB-INF/lib folder and it looks like this:

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

log4j.category.org.springframework.beans.factory=DEBUG

log4j-1.2.17.jar is also in WEB-INF/lib folder. But the following message appears at startup:

log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardServletEnvironment). log4j:WARN Please initialize the log4j system properly.

Please help.

Community
  • 1
  • 1
Aarkan
  • 3,811
  • 6
  • 40
  • 54

3 Answers3

1

add log4j as dependency and put log4j.properties into main/resources - should be working fine.

hi_my_name_is
  • 4,894
  • 3
  • 34
  • 50
1

As you are starting a spring application without web.xml, I suppose you have some initialization class implementing WebApplicationInitializer, and probably extending AbstractAnnotationConfigDispatcherServletInitializer.

If you are not satisfied with the standard log4j.properties at the base of classpath as proposed by freakman, you can overide the onStartup method to configure log4j with a Log4jConfigurer - do not forget to call the base class implementation ...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

You can use it as a bean inside root-context.xml, use classpath for relative path . It works for me

<bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" depends-on="sbeHome">
    <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
    <property name="targetMethod" value="initLogging" />
    <property name="arguments">
        <list>
            <value>classpath:/conf/log4j.xml</value>
        </list>
    </property>
</bean>
ravi ranjan
  • 5,920
  • 2
  • 19
  • 18