86

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>
zb226
  • 9,586
  • 6
  • 49
  • 79
cometta
  • 35,071
  • 77
  • 215
  • 324
  • See also the question [No appenders could be found for logger(log4j)?](http://stackoverflow.com/questions/12532339/no-appenders-could-be-found-for-loggerlog4j#) – PJTraill Jun 15 '16 at 17:24

12 Answers12

44

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
CodeGoat
  • 1,186
  • 8
  • 6
  • 1
    @Kaffiene: it seems it's you who are not reading the entire question (in this case one of the author's comments to the question provide the information to which the answer refers). @CodeGoat: +1 for reading all comments. – Francisco Alvarado May 19 '11 at 17:16
  • @FranciscoAlvarado I have `log4j.properties` file in class path, still warn displays in console. – UdayKiran Pulipati Mar 18 '14 at 08:14
30

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.

Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84
  • 1
    Where can I paste this. – UdayKiran Pulipati Mar 18 '14 at 08:15
  • @udaykiranpulipati you can paste this into log4j.properties. If you don't have log4j.properties create it in src/main/resources/ if using SBT/Gradle/Maven/etc. Otherwise put it somewhere that it will end up at the root of the classpath (maybe in src/). – Alain O'Dea Mar 20 '14 at 18:16
19

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.

slm
  • 15,396
  • 12
  • 109
  • 124
prabhat
  • 191
  • 1
  • 2
  • 1
    I have the same problem in a desktop application (Applet). The file log4j.properties is already in the src folder. How do I "set the output to the bin folder"? – Dilshad Rana Nov 16 '15 at 16:27
7

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

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 2
    i confirm the location is correct . coz when i rename log4j.properties to other name, it give error – cometta Aug 12 '09 at 13:37
7

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".

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
MikeF
  • 71
  • 1
  • 1
5

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);
Krishna
  • 7,154
  • 16
  • 68
  • 80
GARIMA KILLEDAR
  • 141
  • 1
  • 1
4

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.

Marcelo Daniel
  • 143
  • 1
  • 7
3

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> 
cela
  • 2,352
  • 3
  • 21
  • 43
MariemJab
  • 689
  • 8
  • 14
3

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.

Toribio
  • 3,963
  • 3
  • 34
  • 48
jbatista
  • 964
  • 2
  • 11
  • 26
1

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.

1

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>
Innet
  • 459
  • 3
  • 5
  • 18
0

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 .

Roshan Khandelwal
  • 953
  • 1
  • 11
  • 17