我已经把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.

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


当前回答

首先导入:

 import org.apache.log4j.PropertyConfigurator;

然后将以下代码添加到main方法:

String log4jConfPath ="path to/log4j.properties";
PropertyConfigurator.configure(log4jConfPath);

创建一个路径为的文件,并将以下代码添加到该文件中。

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

其他回答

另一个可能发生这种情况的原因(在RCP4中)是您在目标文件中使用了多个日志记录框架。例如,如果您使用slf4j、log4j和ch.qos.logback的组合,就会发生这种情况。Slf4j的目标文件内容选项卡。

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

在配置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

如果你和很多项目一起工作,你可能会面临一个风格问题。

*你必须有一个lof4j。属性文件,此文件包括其他项目的日志属性。

*此外,您可以尝试将log4j属性文件放入src路径,当项目工作在Linux操作系统,其他项目的库和log4。属性文件可以放在一个文件夹下的类路径上的某个位置。

在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...
}