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:
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" />
?
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.
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:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With