log4j:WARN No appenders could be found for logger in web.xml

JavaSpringSpring MvcLog4j

Java Problem Overview


I already put the log4jConfigLocation in web.xml, but I still get the following warning:

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

What did I miss?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/applicationContext.xml
		</param-value>
	</context-param>
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/classes/log4j.properties</param-value>
	</context-param>

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

	<servlet>
		<servlet-name>suara2</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>suara2</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
</web-app>

Java Solutions


Solution 1 - Java

If that's the entire log4j.properties file it looks like you're never actually creating a logger. You need a line like:

log4j.rootLogger=debug,A1

Solution 2 - Java

I had log4j.properties in the correct place in the classpath and still got this warning with anything that used it directly. Code using log4j through commons-logging seemed to be fine for some reason.

If you have:

log4j.rootLogger=WARN

Change it to:

log4j.rootLogger=WARN, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=%5p [%t] (%F:%L) - %m%n

According to http://logging.apache.org/log4j/1.2/manual.html:

> The root logger is anonymous but can be accessed with the Logger.getRootLogger() method. There is no default appender attached to root.

What this means is that you need to specify some appender, any appender, to the root logger to get logging to happen.

Adding that console appender to the rootLogger gets this complaint to disappear.

Solution 3 - Java

You may get this error when your log4j.properties are not present in the classpath.

This means you have to move the log4j.properties into the src folder and set the output to the bin folder so that at run time log4j.properties will read from the bin folder and your error will be resolved easily.

Solution 4 - Java

You'll see this warning if log4j can't find a file "log4j.properties" or "log4j.xml" anywhere.

Solution 5 - Java

Or, you could be doing what I did and define the logger before the log configuration file has been loaded. This would be as they say: "Putting the cart before the horse."

In the code:

public static Logger logger = Logger.getLogger("RbReport");

... later on

PropertyConfigurator.configure(l4j);
logger = Logger.getLogger("RbReport");

Fix was to initialize the logger after the configuration was loaded.

For the geeks it was "Putting Descarte b4 d horse".

Solution 6 - Java

If you want to configure for the standalone log4j applications, you can use the BasicConfigurator. This solution won't be good for the web applications like Spring environment.

You need to write-

BasicConfigurator.configure();

or

ServletContext sc = config.getServletContext();
String log4jLocation = config.getInitParameter("log4j-properties-location");
String webAppPath = sc.getRealPath("/");
String log4jProp = webAppPath + log4jLocation;
PropertyConfigurator.configure(log4jProp);

Solution 7 - Java

If still help, verify the name of archive, it must be exact "log4j.properties" or "log4j.xml" (case sensitive), and follow the hint by "Alain O'Dea". I was geting the same error, but after make these changes everthing works fine. just like a charm :-). hope this helps.

Solution 8 - Java

In my case the solution was easy. You don't need to declare anything in your web.xml.

Because your project is a web application, the config file should be on WEB-INF/classes after deployment. I advise you to create a Java resource folder (src/main/resources) to do that (best pratice). Another approach is to put the config file in your src/main/java.

Beware with the configuration file name. If you are using XML, the file name is log4j.xml, otherwise log4j.properties.

Solution 9 - Java

Put these lines in the beginning of web.xml:

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:/main/resources/log4j.xml</param-value>
</context-param> 

Solution 10 - Java

OK, I see a lot of answer and some very correct. However, none fixed my problem. The problem in my case was the UNIX filesystem permissions had the log4j.properties file I was editing on the server as owned by root. and readable by only root. However, the web application I was deploying was to tomcat couldn't read the file as tomcat runs as user tomcat on Linux systems by default. Hope this helps. so the solution was typing 'chown tomcat:tomcat log4j.properties' in the directory where the log4j.properties file resides.

Solution 11 - Java

Add log4jExposeWebAppRoot -> false in your web.xml. It works with me :)

<context-param>
	<param-name>log4jConfigLocation</param-name>
	<param-value>path/log4j.properties</param-value>
</context-param>
<context-param>
	<param-name>log4jExposeWebAppRoot</param-name>
	<param-value>false</param-value>
</context-param>
<listener>
	<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

Solution 12 - Java

I had the same problem . Same configuration settings and same warning message . What worked for me was : Changing the order of the entries .

  • I put the entries for the log configuration [ the context param and the listener ] on the top of the file [ before the entry for the applicationContext.xml ] and it worked .

The Order matters , i guess .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncomettaView Question on Stackoverflow
Solution 1 - JavaCodeGoatView Answer on Stackoverflow
Solution 2 - JavaAlain O'DeaView Answer on Stackoverflow
Solution 3 - JavaprabhatView Answer on Stackoverflow
Solution 4 - JavaAaron DigullaView Answer on Stackoverflow
Solution 5 - JavaMikeFView Answer on Stackoverflow
Solution 6 - JavaGARIMA KILLEDARView Answer on Stackoverflow
Solution 7 - JavaMarcelo DanielView Answer on Stackoverflow
Solution 8 - JavajbatistaView Answer on Stackoverflow
Solution 9 - JavaMariemJabView Answer on Stackoverflow
Solution 10 - JavaM The DeveloperView Answer on Stackoverflow
Solution 11 - JavaInnetView Answer on Stackoverflow
Solution 12 - JavaRoshan KhandelwalView Answer on Stackoverflow