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:22

This Short introduction to log4j guide is a little bit old but still valid.

That guide will give you some information about how to use loggers and appenders.


Just to get you going you have two simple approaches you can take.

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
查看更多
流年柔荑漫光年
3楼-- · 2018-12-31 10:25

My Eclipse installation could not find log4j.properties when running JUnit tests from Eclipse, even though the file was located at src/test/resources.

The reason was that Eclipse (or the m2e connector) did not copy content from src/test/resources to the expected output folder target/test-classes - the root cause was that in the project's properties under Java Build Path -> Source tab -> Source folders on build path -> src/test/resources, somehow there was an Excluded: ** entry. I removed that excluded entry.

Alternatively, I could have manually copied src/test/resources/log4j.properties to target/test-classes/log4j.properties.

查看更多
何处买醉
4楼-- · 2018-12-31 10:26

Quick solution:

  1. add code to main function:

    String log4jConfPath = "/path/to/log4j.properties";
    PropertyConfigurator.configure(log4jConfPath);
    
  2. 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
    
查看更多
若你有天会懂
5楼-- · 2018-12-31 10:26

You use the Logger in your code to log a message. The Appender is a Object appended to a Logger to write the message to a specific target. There are FileAppender to write to text-files or the ConsoleAppender to write to the Console. You need to show your code of the Logger and Appender setup for more help.

please read the tutorial for a better understanding of the interaction of Logger and Appender.

查看更多
人间绝色
6楼-- · 2018-12-31 10:27

It looks like 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:

  1. Run
  2. Run Configurations
  3. Classpath (tab)
  4. User Entries
  5. Advanced (button on the right)
  6. Add Folders
  7. then navigate to the folder that contains your log4j.properties file
  8. Apply
  9. Run

The error message should no longer appear.

查看更多
大哥的爱人
7楼-- · 2018-12-31 10:27

I faced the same problem when I use log4j2. My problem is caused by using wrong dependent library:

<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <scope>runtime</scope>
    </dependency>

Instead, I should use:

<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <scope>runtime</scope>
    </dependency>

In my case, I have a log4j2.xml defined in my "resources" directory, and specified to use it by:

System.setProperty("log4j.configurationFile", "log4j2.xml");
查看更多
登录 后发表回答