我已经把log4j放到了我的buildpath中,但是当我运行我的应用程序时,我得到了以下消息:

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.

这些警告是什么意思?这里的阑尾是什么?


当前回答

这里的大多数答案都建议使用log4j。属性文件放置在正确的位置(对于maven项目,它应该位于src/main/resources)

但对我来说,问题是log4j。属性配置不正确。这是一个适合我的样品,你可以先试用一下。

# 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

其他回答

当我试图运行单元测试时,在IntelliJ中得到这个错误消息。我同时打开了Eclipse和IntelliJ,这可能导致了这个问题。在我关闭两个IDE并运行maven命令行构建后,错误就消失了。

在java eclipse中将conf_ref复制到conf文件夹中。

如果log4j。你正在使用Spring Boot创建一个WAR文件部署到应用服务器上,你省略了一个web.xml文件来支持Spring Boot的自动配置,并且你没有得到任何日志消息,你需要显式地配置Log4j。假设您正在使用Log4j 1.2.x:

public class AppConfig extends SpringBootServletInitializer {

    public static void main( String[] args ) {
        // Launch the application
        ConfigurableApplicationContext context = SpringApplication.run( AppConfig.class, args );
    }

    @Override
    protected SpringApplicationBuilder configure( SpringApplicationBuilder application ) {
        InputStream log4j = this.getClass().getClassLoader().getResourceAsStream("log4j.properties");
        PropertyConfigurator.configure(log4j);
        return application;
    }

// Other beans as required...
}

我的Eclipse安装找不到log4j。属性,即使该文件位于src/test/resources。

原因是Eclipse(或m2e连接器)没有将内容从src/test/resources复制到预期的输出文件夹target/test-classes -根本原因是在Java Build Path -> Source选项卡-> Build Path -> src/test/resources下的项目属性中,有一个Excluded: **条目。我去掉了那个被排除的元素。

或者,我可以手动复制src/test/resources/log4j。目标/test-classes/log4j.properties中的属性。

得到同样的错误。下面是导致错误信息的问题:

在配置log4j之前,我创建了一些使用Logger的对象:

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

解决方案: 在main方法的开头配置log4j:

PropertyConfigurator.configure(xmlLog4JConfigFile); 
// or BasicConfigurator.configure(); if you dont have a config file