Log4j Warning while initializing? [duplicate]

I am trying to learn about log4j so I just tried to do something which is very simple;

Logger logger = Logger.getLogger("ClientApplicationLog");
logger.info("Logger Test");

But after making this I got;

log4j:WARN No appenders could be found for logger (ClientApplicationLog).
log4j:WARN Please initialize the log4j system properly.

Do you know where I am wrong ?

Thank you all


You're missing the log4j.properties or log4j.xml in your classpath.

You can bypass this by using

BasicConfigurator.configure();

But beware this will ONLY log to System.out and is not recommended. You should really use one of the files above and write to a log file.

A very simple example of log4j.properties would be

#Log to Console as STDOUT
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c %3x - %m%n
#Log to file FILE
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=logfile.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.append=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c %3x - %m%n

#Root Logger
log4j.rootLogger=INFO, stdout, file

You need to have a log4j.xml somewhere in your class path with information telling it where to log, how to log etc. Alternatively you can set all this programmatically in your code, but it's much nicer to have the flexibility in your implementation.

My log4j.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<!-- ============================== -->
   <!-- Append messages to the console -->
   <!-- ============================== -->

   <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
      <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
      <param name="Target" value="System.out"/>
      <param name="Threshold" value="DEBUG"/>

      <layout class="org.apache.log4j.PatternLayout">
         <!-- The default pattern: Date Priority [Category] Message\n -->
         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
      </layout>
   </appender>

<!-- ======================= -->
   <!-- Setup the Root category -->
   <!-- ======================= -->

   <root>
      <!-- 
         Set the root logger priority via a system property. Note this is parsed by log4j         
       -->
      <appender-ref ref="CONSOLE"/>
   </root>

</log4j:configuration>

This is just the warning.

Fixing

This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration.

To fix that, simply create/copy log4j.properties or log4j.xml into your a location on the classpath (usually the same as the jar files).

Optionally set java option: -Dlog4j.configuration=file:///path/to/log4j.properties.

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.

Debugging

For debugging, you may try to use -Dlog4j.debug=true parameter.

Configuration of log4j.properties

Sample configuration of log4j.properties:

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

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

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN

Here is another configuration file that uses multiple appenders:

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

Apache Solr

If using Solr, copy <solr>/example/resources/log4j.properties into a location on the classpath.

Sample configuration of log4j.properties from Solr goes like:

#  Logging level
solr.log=logs/
log4j.rootLogger=INFO, file, CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n

#- size rotation with log cleanup.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=4MB
log4j.appender.file.MaxBackupIndex=9

#- File to log to and log format
log4j.appender.file.File=${solr.log}/solr.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS}; %C; %m\n

log4j.logger.org.apache.zookeeper=WARN
log4j.logger.org.apache.hadoop=WARN

# set to INFO to enable infostream log messages
log4j.logger.org.apache.solr.update.LoggingInfoStream=OFF

See also:

  • Short introduction to log4j: Default Initialization Procedure
  • Why can't log4j find my properties in a J2EE or WAR application?

You need to define an appender for your logger (e.g. a log file), e.g. in a log4j.properties files on the classpath.

This tutorial should contain everything you need to know.