Log4j, Spring MVC, no add-ons found for logger

Log4j is working fine, however, when I start the server, I get the following 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. 

This means that log4j.properties cannot be found. But I'm not sure how to fix it, because everything is working fine.

+7
source share
4 answers

Put 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 the location of the log4j properties in the param context, but you do not need to do this if it is placed in the classpath.

Example:

 <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/resources/log4j.properties</param-value> </context-param> 
+17
source

Move these 2 lines of code at the top of your web.xml to solve 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> 
+6
source

If you use eclipse and tomcat for your development, the 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 a 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 create an instance of the registrar

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

after that you can use the recorder anywhere

 logger.debug("this will appear as a log in mylogfile.log!"); 
+1
source

I configured <filter> in my web.xml which was creating the problem. I commented on this, and then it worked for me!

0
source

Source: https://habr.com/ru/post/910634/


All Articles