Thursday, July 25, 2013

maven and log4j - log4j:WARN No appenders could be found for logger


Problem

When running a test you get the following warning message:

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



Solution

To get rid of that warning message,  just set log4's configuration file under the right folder.
Example:






An example of log4.xml configuration file:

<?xml version="1.0" encoding="UTF-8" ?>
<!-- <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> -->
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="[temp01] %p [%t] %c{1}.%M(%L) | %m%n"/>
        </layout>
    </appender>

    <logger name="net.sf.ehcache">
        <level value="ERROR"/>
    </logger>

    <!-- Suppress success logging from InteractiveAuthenticationSuccessEvent -->
    <logger name="org.acegisecurity">
        <level value="ERROR"/>
    </logger>

    <logger name="org.apache">
        <level value="WARN"/>
    </logger>

    <logger name="org.hibernate">
        <level value="WARN"/>
    </logger>
 
    <!--logger name="org.hibernate.SQL">
        <level value="DEBUG"/>
    </logger-->

    <logger name="org.springframework">
        <level value="WARN"/>
    </logger>

    <!-- Suppress warnings from Commons Validator -->
    <logger name="org.apache.commons.validator.ValidatorResources">
        <level value="ERROR"/>
    </logger>

    <logger name="org.appfuse">
        <level value="INFO"/>
    </logger>
   
    <logger name="br.com.adr">
        <level value="DEBUG"/>
    </logger>

    <root>
        <level value="WARN"/>
        <appender-ref ref="CONSOLE"/>
    </root>

</log4j:configuration>




----------------------------------------------------------------------------------

 An example of WRONG place:





---------------------------------------------------------------------------------------------------
EXTRA


Why do I see a warning about "No appenders found for logger" and "Please configure log4j properly"? 
This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit
configuration.
log4j uses Thread.getContextClassLoader().getResource() to locate the default configuration files and does not directly check the file system.
Knowing the appropriate location to place log4j.properties or log4j.xml requires understanding the search strategy of the class loader in use.
log4j does not provide a default configuration since output to the console or to the file system may be prohibited in some environments.
Also see FAQ: Why can't log4j find my properties in a J2EE or WAR application?.

Why can't log4j find my properties file in a J2EE or WAR application?

The short answer: the log4j classes and the properties file are not within the scope of the same classloader.
The long answer (and what to do about it): J2EE or Servlet containers utilize Java's class loading system.
Sun changed the way classloading works with the release of Java 2.
In Java 2, classloaders are arranged in a hierarchial parent-child relationship.
When a child classloader needs to find a class or a resource, it first delegates the request to the parent.
Log4j only uses the default Class.forName() mechanism for loading classes.
Resources are handled similarly.
See the documentation for java.lang.ClassLoader for more details.
So, if you're having problems, try loading the class or resource yourself.
If you can't find it, neither will log4j.


FROM:

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



No comments:

Post a Comment

eclipse: java: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" or Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

  >PROBLEM Using Eclipse, you try to run a simple logging test using "org.slf4j.Logger" like the sample below: package Test; im...