7

The log4j is working fine, however, at server startup, I am getting these warnings:

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.

It's implying the log4j.properties could not be found. But I am not sure how to fix that because everything seems to be working fine.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
l a s
  • 3,836
  • 10
  • 42
  • 61

4 Answers4

17

Place the spring Log4jConfigListener as the first listener in your web.xml.

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> 

You set upp the location of the log4j properties in a context-param but you do not need to do this if it is placed on the classpath.

An example is ..

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/resources/log4j.properties</param-value>
</context-param>
6

Move these 2 lines of code to the top of your web.xml to resolve the problem.

<!-- location of log4j config file -->
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
</context-param>

<!-- applies log4j configuration -->
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
JohnP
  • 125
  • 1
  • 5
1

If you are using eclipse and tomcat for your development, following method should work

Declare the following dependency in your pom.xml

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

Create log4j.properties file and put it in the resources folder ("src/main/resources")

# Root logger option
log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#outputs to Tomcat home
log4j.appender.file.File=${catalina.home}/logs/mylogfile.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

And then in your class, instantiate logger

private static final Logger logger = Logger.getLogger(WelcomeController.class);

after that you can use logger any where you want

logger.debug("this will appear as a log in mylogfile.log!");
Dileep
  • 29
  • 3
0

I had configured <filter> in my web.xml file which was creating problem. I commented that out, and then it worked for me!

Bob Dalgleish
  • 8,167
  • 4
  • 32
  • 42
CodeWorld
  • 2,017
  • 1
  • 11
  • 21