Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j.xml - WARN No appenders could be found for logger

Tags:

xml

log4j

I ltried to use log4j loger for my simple project.

But when I run project I cought strainge warning from log4j at console:

log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

I couldn't figure out why exactly this happen.

I added and downloaded log4j with Maven all should work.

I specified apeender, to my mind ok it should work correctly.
But as you can see it isn't.

Here is content of my log4j.xml:

<!-- Appenders -->
<!-- Loggin into console -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%-5p: %c - %m%n" />
    </layout>
</appender>
<appender name="file" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="file" value="/logs/log.log" />
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p (%F:%L:%M)  %c{1}  - %m%n" />
    </layout>
</appender>

<!-- Root logger -->
<root>
    <priority value="info" />
    <appender-ref ref="file" />
    <appender-ref ref="console" />
</root>

<!-- Application Loggers -->
<logger name="com.softserve.edu">
    <level value="info" />
</logger>

I missed standart header and footer ( tags).

One slippery palce is that this file into logs folder doesn't exist (I guess that log4j will create it by itself).

Here is my project struckture:

project struckture

UPDATE:

I created new folder under src/test/resources and moved log4j.xml into.

Now all are ok with this WARNING but log file doesn't created.

what is wrong with this line <param name="file" value="/logs/log.log" /> ?

  • How to solve this trouble?
like image 692
catch23 Avatar asked Oct 03 '22 08:10

catch23


2 Answers

Your log4j.xml file needs to go in the directory src/main/resources. Log4J is expecting to find it at the root of the classpath.

The resources directory is the place to put everything that is not Java code, but which you want to go into the classpath. If it's something only tests should use, put it in src/test/resources, otherwise use src/main/resources.

like image 157
Nathan Hughes Avatar answered Oct 13 '22 09:10

Nathan Hughes


I ended up struggling to answer this one myself, so: from the l0g4j site, this is an FAQ, which the error actually points you to.

http://logging.apache.org/log4j/1.2/faq.html#noconfig

As you're aware (but for the layman who stumbles across this common error): An Appender is where the output goes. You can have console Appenders, file Appenders, and others which are all spelled out in the directions.

When log4j tells you it has no Appenders registered, it's telling you that the output has nowhere to go. This is why, presumably, your application is silent except for the error.

Given that you have, indeed, specified Appenders the error can really be only one of a few things.

If the Appender was somehow malformed, you'd expect to see a Parsing exception, so that's probably not the issue.

IF, however, the log configuration file could not be found AT ALL.....you'd see exactly this problem! More than likely your application simply does not see the configuration file at all and is attempting to resort to defaults.

How you could get in this state includes:

  • Failure to comply with the maven convention, or supply an alternative configuration. (The solution to which is found in Nathan Hughes' answer).
  • Specifying an alternative Maven Configuration that seems correct (enough that maven runs) but fails to do what you think it do.
  • Configuring Log4j in-code to look for a file that isn't visible to the application

When you put the file into ./src/main/resources Maven actually bundles the logger.xml INTO the jar. Which puts it on the classpath, which is where the logger is looking by default (obviously the jar is on the classpath).

However, many users don't want to see the logging configuration packaged inside of their "binary" jar. What's the point of configuring the log-level if you have to either recompile, or essentially do messy in-place editing of a configuration inside of a zip file? So, for many Nathan Hughes' answer is a non-answer, which is why its worth talking about the other scenarios.

If you've packaged your configuration file WITH the jar then the software is not looking in the right place. If you've done any work to move the file yourself, then perhaps you've hidden the log4j.xml file from yourself.

However, most likely you packaged the file by using the maven convention ./src/main/config/log4j.xml. If this is the case, then the problem is that your software is not configured to look for it.

One method is to specify on the command line when invoking your application

-Dlog4j.configuration=file:conf/log4j.xml 

If you're a fan of hard code (I'm not) SOMEWHERE in your code, should be a call

For a .properties file:

File log4jfile = new File("./conf/log4j.properties");
PropertyConfigurator.configure(log4jfile.getAbsolutePath());

For a .xml file:

DOMConfigurator.configure("file:///conf/log4j.xml");
Logger log = Logger.getLogger(YourClass.class);
like image 42
InfernalRapture Avatar answered Oct 13 '22 11:10

InfernalRapture