No appenders could be found for logger(log4j)?

2018-12-31 10:05发布

I have put log4j to my buildpath, but 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.

What do these warnings mean? Whats the appender here?

26条回答
若你有天会懂
2楼-- · 2018-12-31 10:38

Maybe add the relevent project contains log4j in java build path, I add mahout_h2o into it when I met this problem in a mahout project using eclipse, it works!

查看更多
初与友歌
3楼-- · 2018-12-31 10:38

I had this problem too. I just forgot to mark the resources directory in IntelliJ IDEA

  1. Rightclick on your directory
  2. Mark directory as
  3. Resources root
查看更多
泛滥B
4楼-- · 2018-12-31 10:39

Make sure the properties file has properly set. And again, it seems like that the compiler cannot find the properties file, you can set as follow at the pom (only when you use maven project).

<build>
       <sourceDirectory> src/main/java</sourceDirectory>
       <testSourceDirectory> src/test/java</testSourceDirectory>
        <resources>
             <resource>
                  <directory>resources</directory>
             </resource>
        </resources>           
</build >
查看更多
低头抚发
5楼-- · 2018-12-31 10:40

I think you should understand where the log4j jar file or the Java code looks for the log4j configuration files.

src/main/resources/log4j.properties is Eclipse path. Place them in a proper location so that you don't have to hard code the absolute path in code.

Read my article and sample solution for that http://askyourquestions.info/2016/03/27/how-to-see-where-the-log-is-logger-in-slf4j/

查看更多
牵手、夕阳
6楼-- · 2018-12-31 10:44

I get the same error. Here the problem which leads to this error message:

I create some objects which use the Logger before I configure the log4j:

Logger.getLogger(Lang.class.getName()).debug("Loading language: " + filename);

Solution: Configure the log4j at the beginning in the main method:

PropertyConfigurator.configure(xmlLog4JConfigFile); 
// or BasicConfigurator.configure(); if you dont have a config file
查看更多
弹指情弦暗扣
7楼-- · 2018-12-31 10:45

Most of the answers here suggested that log4j.properties file be placed in the right location(for maven project, it should be located in src/main/resources)

But for me, the problem is that my log4j.properties is not correctly configured. Here's a sample that works for me, you can try it out first.

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to 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{1}:%L - %m%n
查看更多
登录 后发表回答