logging器(log4j)没有findappender?

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

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

这个简短的介绍log4j指南是有点老,但仍然有效。

该指南将给你一些关于如何使用logging器和appender的信息。


为了让你走,你有两个简单的方法,你可以采取。

首先是将这一行添加到您的主要方法:

 BasicConfigurator.configure(); 

第二种方法是将这个标准的log4j.properties (取自上面提到的指南)文件添加到你的类path中:

 # 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 

看起来您需要将您的log4j.properties文件的位置添加到Eclipse中的Classpath中。

确保您的项目在Eclipse中打开,然后单击Eclipse顶部的“运行”菜单,然后单击以下选项:

  1. 运行configuration
  2. 类path(选项卡)
  3. 用户条目
  4. 高级(button在右边)
  5. 添加文件夹
  6. 然后导航到包含您的log4j.properties文件的文件夹
  7. 应用

错误消息不应该再出现。

快速解决:

  1. 添加代码到主要function

     String log4jConfPath = "/path/to/log4j.properties"; PropertyConfigurator.configure(log4jConfPath); 
  2. / path / to中创build一个名为log4j.properties的文件

     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 

这只是一个警告。

定影

如果找不到默认configuration文件log4j.propertieslog4j.xml ,并且应用程序不执行显式configuration,则会发生这种情况。

要解决这个问题,只需将log4j.propertieslog4j.xml创build/复制到类path中的某个位置(通常与jar文件相同)即可。

可以select设置java选项: -Dlog4j.configuration=file:///path/to/log4j.properties

log4j使用Thread.getContextClassLoader().getResource()来定位默认configuration文件,并不直接检查文件系统。 了解要放置log4j.propertieslog4j.xml的适当位置,需要了解正在使用的类加载器的search策略。 log4j不提供默认configuration,因为在某些环境中可能禁止输出到控制台或文件系统。

debugging

对于debugging,您可以尝试使用-Dlog4j.debug=true参数。

configurationlog4j.properties

log4j.properties示例configuration:

 # 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 # Print only messages of level WARN or above in the package com.foo. log4j.logger.com.foo=WARN 

这是另一个使用多个appender的configuration文件:

 log4j.rootLogger=debug, stdout, R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=example.log log4j.appender.R.MaxFileSize=100KB # Keep one backup file log4j.appender.R.MaxBackupIndex=1 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n 

Apache Solr

如果使用Solr ,请将<solr>/example/resources/log4j.properties复制到类path中的某个位置。

来自Solr的log4j.properties示例configuration如下所示:

 # Logging level solr.log=logs/ log4j.rootLogger=INFO, file, CONSOLE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n #- size rotation with log cleanup. log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.MaxFileSize=4MB log4j.appender.file.MaxBackupIndex=9 #- File to log to and log format log4j.appender.file.File=${solr.log}/solr.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS}; %C; %m\n log4j.logger.org.apache.zookeeper=WARN log4j.logger.org.apache.hadoop=WARN # set to INFO to enable infostream log messages log4j.logger.org.apache.solr.update.LoggingInfoStream=OFF 

也可以看看:

  • 对log4j的简短介绍:默认初始化过程
  • 为什么log4j不能在J2EE或WAR应用程序中find我的属性?

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

请阅读教程 ,以更好地了解logging器和Appender的交互。

如前所述,有两种方法

首先是将这一行添加到您的主要方法:

 BasicConfigurator.configure(); 

第二种方法是将这个标准的log4j.properties文件添加到你的类path中:

采取第二种方法时,你需要确保你正确地初始化文件,例如。

 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"); 

确保您创build了存储日志文件所需的文件夹。

我得到同样的错误。 这里导致这个错误信息的问题:

在configurationlog4j之前,我创build了一些使用Logger的对象:

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

解决scheme:在主要方法的开头configurationlog4j:

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

我想你应该知道log4j jar文件或Java代码在哪里查找log4jconfiguration文件。

src/main/resources/log4j.properties是Eclipsepath。 把它们放在适当的位置,这样你就不必在代码中硬编码绝对path。

阅读我的文章和示例解决scheme, http://askyourquestions.info/2016/03/27/how-to-see-where-the-log-is-logger-in-slf4j/

这里的大部分答案都build议log4j.properties文件应该放在正确的位置(对于maven项目,它应该位于src/main/resources

但对我来说,问题是我的log4j.propertiesconfiguration不正确。 这里有一个适合我的示例,你可以先试试。

 # 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 

确保属性文件已经正确设置。 再次,似乎编译器找不到属性文件,你可以在pom中设置如下(仅在使用maven项目时)。

 <build> <sourceDirectory> src/main/java</sourceDirectory> <testSourceDirectory> src/test/java</testSourceDirectory> <resources> <resource> <directory>resources</directory> </resource> </resources> </build > 

在我的情况下,错误是标志“可加性 ”。 如果你的根项目包是“false”,那么子包将没有appender,你会看到“ appender not found ”错误。

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

我试图在intellij 12中用maven构build一个可执行的jar时遇到了这个问题。事实certificate,因为java清单文件没有包含类path,所以无法在根级别findlog4j属性文件jar文件被执行。)

仅供参考我正在得到这样的logging器:

 Logger log = LogManager.getLogger(MyClassIWantedToLogFrom.class); 

我能够得到它与一个包括这样的POM文件:

  <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.mycompany.mainPackage.mainClass</mainClass> </manifest> <manifestEntries> <Class-Path>.</Class-Path> <!-- need to add current directory to classpath properties files can be found --> </manifestEntries> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> 

确保您的项目在Eclipse中打开,然后单击Eclipse顶部的“运行”菜单,然后单击以下选项:

  1. 运行configuration

  2. 类path(选项卡)

  3. 用户条目

  4. 在右边添加jar

  5. 添加log4j jar文件

  6. 应用

错误消息不应该再出现。

原因可能是一些static词缺乏:

 final static Logger logging = Logger.getLogger(ProcessorTest.class); 

如果我把logging器作为实例字段,我正在得到这个非常警告:

 No appenders could be found for logger (org.apache.kafka.producer.Sender) 

更糟糕的是,这个警告并没有指出错误所在的ProcessorTest ,而是一个完全不同的类(Sender)作为问题的根源。 那个类有正确的设置logging器,不需要任何改变! 我们可以寻找这个问题的年龄!

当我使用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"); 
 Properties prop = new Properties(); prop.setProperty("log4j.rootLogger", "WARN"); PropertyConfigurator.configure(prop); 

即使文件位于src/test/resources ,我的Eclipse安装在从Eclipse运行JUnittesting时也找不到log4j.properties

原因是Eclipse(或m2e连接器)没有将src/test/resources中的内容复制到预期的输出文件夹target/test-classes – 根本原因是在Java Build Path – > Source选项卡下的项目属性中, > 构buildpath上的源文件夹 – > src / test / resources ,不知何故,有一个Excluded: **条目。 我删除了排除的条目。

或者,我可以手动将src/test/resources/log4j.properties复制到target/test-classes/log4j.properties

如果log4j.properties确实在类path中,那么您使用的是Spring Boot来创buildWAR文件以部署到应用程序服务器,您省略了一个web.xml文件来支持Spring Boot的自动configuration,并且您没有收到任何日志消息无论如何,你需要明确地configurationLog4j。 假设你正在使用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... } 

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