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

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


当前回答

这个网站上的解决方案为我工作https://crunchify.com/java-how-to-configure-log4j-logger-property-correctly/。现在我在log4j中没有看到任何警告

我把它放在log4j中。我把它放在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

其他回答

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

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

我在使用log4j2时也遇到了同样的问题。我的问题是由使用错误的依赖库引起的:

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

相反,我应该使用:

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

在我的情况下,我有一个log4j2.xml定义在我的“资源”目录,并指定使用它:

System.setProperty("log4j.configurationFile", "log4j2.xml");

在代码中使用Logger记录消息。Appender是附加到Logger的对象,用于将消息写入特定目标。FileAppender用于写入文本文件,ConsoleAppender用于写入控制台。您需要显示Logger和Appender设置的代码以获得更多帮助。

为了更好地理解Logger和Appender的交互,请阅读教程。

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

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