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

Add the following as the first code:

Properties prop = new Properties();
prop.setProperty("log4j.rootLogger", "WARN");
PropertyConfigurator.configure(prop);
查看更多
怪性笑人.
3楼-- · 2018-12-31 10:29

In my case, the error was the flag "additivity". If it's "false" for you root project package, then the child packages will not have an appender and you will see the "appender not found" error.

查看更多
其实,你不懂
4楼-- · 2018-12-31 10:30

Another reason this may happen (in RCP4) is that you are using multiple logging frameworks in your target file. As an example, this will occur if you use a combination of slf4j, log4j and ch.qos.logback.slf4j in your target files content tab.

查看更多
皆成旧梦
5楼-- · 2018-12-31 10:30

In java eclipse copy your conf_ref to conf folder.

查看更多
荒废的爱情
6楼-- · 2018-12-31 10:33

The solution on this site worked for me https://crunchify.com/java-how-to-configure-log4j-logger-property-correctly/. I now see no warnings at all from log4j

I put this in a log4j.properties file that I put in src/main/resources

# This sets the global logging level and specifies the appenders
log4j.rootLogger=INFO, theConsoleAppender

# settings for the console appender
log4j.appender.theConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.theConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.theConsoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
查看更多
还给你的自由
7楼-- · 2018-12-31 10:35

As explained earlier there are 2 approaches

First one is to just add this line to your main method:

BasicConfigurator.configure();

Second approach is to add this standard log4j.properties file to your classpath:

While taking second approach you need to make sure you initialize the file properly, Eg.

Properties props = new Properties();
props.load(new FileInputStream("log4j property file path"));
props.setProperty("log4j.appender.File.File", "Folder where you want to store log files/" + "File Name");

Make sure you create required folder to store log files.

查看更多
登录 后发表回答