DEV Community

Mohammad Faisal
Mohammad Faisal

Posted on • Edited on

Minimum Configuration for log4j

No appenders could be found for logger(log4j)

You may have encountered this issue with many applications (hibernate, spring, etc.) when trying to run a Java program from command line.
The error appears in console something like this:

log4j:ERROR Could not find value for key log4j.appender.CONSOLE
log4j:ERROR Could not instantiate appender named "CONSOLE".

log4j:WARN No appenders could be found for logger (com.company.module.ClassName).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Enter fullscreen mode Exit fullscreen mode

As the warning says itself: Please initialize the log4j system properly, you must check when running the program did the log4j.properties or log4j.xml is available on the classpath or either one of them do really exists?

A minimum configuration would be

log4j.properties
# Set root logger level to DEBUG and its only appender to CONSOLE.
log4j.rootLogger=DEBUG, CONSOLE

# CONSOLE appender is set to be a ConsoleAppender.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

# CONSOLE appender uses PatternLayout.
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Enter fullscreen mode Exit fullscreen mode
log4j.xml
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'>

  <appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out"/>
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
    </layout>
  </appender>

  <root>
    <priority value ="debug"></priority>
    <appender-ref ref="console"></appender>
  </root>
</log4j:configuration>
Enter fullscreen mode Exit fullscreen mode

You can name anything for the appender name which I had chosen CONSOLE/console as this is more relevant if you want to see the logs in console rather than appended in some file.

Application specific log configuration

log4j.logger.domain.company.name=<LOG_LEVEL>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)