...let John help you code



Error: No appenders could be found for logger log4j

Asked by:
mod5
Nov 20, 2016, 17:01

Description

I get the following message when I run my application:
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.

2 Answers

Answer :
Code

Preview
 

6

You can try two simple approaches:

First one is to just add this line to your main method:
BasicConfigurator.configure();  

Second approach is to add this standard log4j.properties (taken from the above mentioned guide) file 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
Mandy276


2

You need to add the location of your log4j.properties file to the Classpath in Eclipse.

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

  • Run

  • Run Configurations

  • Classpath (tab)

  • User Entries

  • Advanced (button on the right)

  • Add Folders
    then navigate to the folder that contains your log4j.properties file

  • Apply

  • Run


  • Alternate solution



    add code to main function:
    String log4jConfPath = "/path/to/log4j.properties";
    PropertyConfigurator.configure(log4jConfPath);

    create a file named log4j.properties at /path/to
    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
    selPat