** This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details **

Question:

I put log4j in my build path, but when I run the program I report the following message:

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

What do these warn mean? What is appenders here?

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — the following for the answer, according to support sorting votes — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Answer 1 (506 votes) : This brief introduction to the Log4J guide is old, but still valid. This guide will provide information on how to use Loggers and appenders.

Here are two simple methods you can follow. The first is to add this line to your main method:

BasicConfigurator.configure();
Copy the code

The second method is to add the configuration file log4j.properties (retrieved from the above guide) to your classpath:

# 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
Copy the code

Answer 2 (78 votes) : It looks like you need to add the path to the log4j.properties file to your Classpath in Eclipse.

Make sure your project is open in Eclipse, then click the Run menu at the top of Eclipse, and then click the following:

  1. Run
  2. Run Configurations
  3. Classpath (tab)
  4. User Entries
  5. Advanced (button on left)
  6. Add Folders
  7. Then navigate to the folder containing the log4j.properties file
  8. log4j.properties file
  9. Apply
  10. Run

There should be no more error messages.

Answer 3 (52 votes in favour) :

52

Quick solution:

  1. tomain functionAdd the following code to:
String log4jConfPath = "/path/to/log4j.properties";
PropertyConfigurator.configure(log4jConfPath);
Copy the code
  1. Create a file named log4j.properties under the /path/to path
log4j.rootLogger=INFO, 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{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
Copy the code