[英]No appenders could be found for logger(log4j)?
我已将 log4j 放入我的构建路径,但在运行我的应用程序时收到以下消息:
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是什么?
该指南将为您提供有关如何使用记录器和附加器的一些信息。
为了让您继续前进,您可以采取两种简单的方法。
第一个是将此行添加到您的主要方法中:
BasicConfigurator.configure();
第二种方法是将此标准log4j.properties
(取自上述指南)文件添加到您的类路径中:
# 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 中的类路径。
确保您的项目在 Eclipse 中打开,然后单击 Eclipse 顶部的“运行”菜单并单击以下内容:
错误消息不应再出现。
快速解决方案:
将代码添加到主函数:
String log4jConfPath = "/path/to/log4j.properties"; PropertyConfigurator.configure(log4jConfPath);
在/path/to创建一个名为 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
这只是一个警告。
当无法找到默认配置文件log4j.properties
和log4j.xml
并且应用程序未执行显式配置时,就会发生这种情况。
要解决这个问题,只需将log4j.properties
或log4j.xml
创建/复制到类路径上的位置(通常与 jar 文件相同)。
可选择设置 java 选项: -Dlog4j.configuration=file:///path/to/log4j.properties
。
log4j
使用Thread.getContextClassLoader().getResource()
定位默认配置文件,不直接检查文件系统。 知道放置log4j.properties
或log4j.xml
的适当位置需要了解正在使用的类加载器的搜索策略。log4j
不提供默认配置,因为在某些环境中可能禁止输出到控制台或文件系统。
对于调试,您可以尝试使用-Dlog4j.debug=true
参数。
log4j.properties
配置 log4j.properties
示例配置:
# 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 的配置文件:
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
如果使用Solr ,请将<solr>/example/resources/log4j.properties
复制到classpath上的某个位置。
Solr 中log4j.properties
示例配置如下:
# 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.properties
文件放在正确的位置(对于 maven 项目,它应该位于src/main/resources
)
但对我来说,问题是我的log4j.properties
没有正确配置。 这是一个对我有用的示例,您可以先尝试一下。
# 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
如前所述,有两种方法
第一个是将此行添加到您的主要方法中:
BasicConfigurator.configure();
第二种方法是将此标准log4j.properties文件添加到您的类路径:
在采用第二种方法时,您需要确保正确初始化文件,例如。
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");
确保创建所需的文件夹来存储日志文件。
您可以在代码中使用Logger
来记录消息。 Appender
是附加到Logger
的对象,用于将消息写入特定目标。 有FileAppender
写入文本文件或ConsoleAppender
写入控制台。 您需要显示 Logger 和 Appender 设置的代码以获得更多帮助。
请阅读教程以更好地理解 Logger 和 Appender 的交互。
确保属性文件已正确设置。 再次,编译器似乎找不到属性文件,您可以在pom中设置如下(仅当您使用maven项目时)。
<build>
<sourceDirectory> src/main/java</sourceDirectory>
<testSourceDirectory> src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>resources</directory>
</resource>
</resources>
</build >
我犯了同样的错误。 这是导致此错误消息的问题:
在配置 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
当我尝试运行 JUnit 测试类时,我遇到了同样的问题。
在 src/test/resources 文件夹中手动添加 log4j.properties 文件后,问题得到解决。
将以下代码添加到 log4j.properties 文件解决了该问题:
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# 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
添加以下作为第一个代码:
Properties prop = new Properties();
prop.setProperty("log4j.rootLogger", "WARN");
PropertyConfigurator.configure(prop);
我认为您应该了解 log4j jar 文件或 Java 代码在哪里查找 log4j 配置文件。
src/main/resources/log4j.properties
是 Eclipse 路径。 将它们放在适当的位置,这样您就不必在代码中硬编码绝对路径。
阅读我的文章和示例解决方案https://askyourquestions.info/how-to-see-where-the-log-is-logger-in-slf4j/
这可能发生的另一个原因(在 RCP4 中)是您在目标文件中使用了多个日志框架。 例如,如果您在目标文件内容选项卡中使用 slf4j、log4j 和 ch.qos.logback.slf4j 的组合,就会发生这种情况。
就我而言,错误是标志“可加性”。 如果根项目包为“false”,则子包将没有附加程序,您将看到“找不到附加程序”错误。
如果您正在使用 Eclipse 并且在一切正常后突然出现此问题,请尝试转到Project - Clean - Clean
。
我在 Intellij 12 中尝试使用 maven 构建可执行 jar 时遇到了这个问题。结果是因为 java 清单文件不包含类路径,所以在根级别找不到 log4j 属性文件(其中jar 文件是从中执行的。)
仅供参考,我得到了这样的记录器:
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 顶部的“运行”菜单并单击以下内容:
跑
运行配置
类路径(选项卡)
用户条目
在右边添加罐子
添加 log4j jar 文件
申请
跑
错误消息不应再出现。
原因可能是在某些情况下缺少static
一词:
final static Logger logging = Logger.getLogger(ProcessorTest.class);
如果我将 logger 作为实例字段,我会收到非常警告:
No appenders could be found for logger (org.apache.kafka.producer.Sender)
更糟糕的是,警告不是指向错误所在的ProcessorTest
,而是指向一个完全不同的类(Sender)作为问题的根源。 这个类有正确的一组记录仪,不用任何变化! 我们可以寻找多年的问题!
我在使用 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");
当 Log4j Java 代码正在搜索以在您的程序中创建第一个日志行时,Log4J 会显示此警告消息。
此时,Log4j 做了两件事
log4j.properties
文件log4j.properties
定义的 appender 如果log4J
找不到log4j.properties
文件,或者如果log4j.rootlogger
中声明的 appender 未在log4j.properties
文件中的其他地方定义,则会显示警告消息。
注意:Properties 文件的内容必须正确。
以下内容不正确
log4j.rootLogger=file
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=c:/Trace/MsgStackLogging.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%m%n
log4j.appender.FILE.ImmediateFlush=true
log4j.appender.FILE.Threshold=debug
log4j.appender.FILE.Append=false
因为file
appender 在log4j.rootlogger
语句中以 LOWER-CASE 声明,并在 log4j.appender 语句中使用 UPPER-CASE 定义!
正确的文件是
log4j.rootLogger=FILE
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=c:/Trace/MsgStackLogging.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%m%n
log4j.appender.FILE.ImmediateFlush=true
log4j.appender.FILE.Threshold=debug
log4j.appender.FILE.Append=false
如果使用 MAVEN,则必须将 log4j.properties 文件放在src/main/resources
并启动 MAVEN 构建。
然后将 Log4j.properties 文件复制到target/classes
文件夹中。
Log4J 使用它在target/classes
找到的log4j.properties
文件!
我也有这个问题。 我只是忘记在 IntelliJ IDEA 中标记资源目录
首先:创建一个 log4j.properties 文件
# 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
把它放在 src/main/resources/
之后,使用这两个依赖项:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
有必要将这个最终依赖添加到 POM 文件中:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
从 Eclipse 运行JUnit测试时,我的 Eclipse 安装找不到log4j.properties
,即使该文件位于src/test/resources
。
原因是 Eclipse(或m2e连接器)没有将内容从src/test/resources
复制到预期的输出文件夹target/test-classes
- 根本原因是在Java Build Path -> Source选项卡下的项目属性中 - >构建路径上的源文件夹-> src/test/resources ,不知何故有一个Excluded: **
条目。 我删除了那个排除的条目。
或者,我可以手动将src/test/resources/log4j.properties
复制到target/test-classes/log4j.properties
。
如果log4j.properties
确实在类路径上,则您正在使用 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...
}
也许在java构建路径中添加相关项目包含log4j,当我在使用eclipse的mahout项目中遇到这个问题时,我将mahout_h2o添加到其中,它有效!
如果你和很多项目一起工作,你可能会面临风格问题。
*您必须有一个 lof4j.properties 文件,并且该文件包含其他项目的日志属性。
*另外你可以尝试在项目运行Linux操作系统时将log4j属性文件放入src路径,其他项目的libs和log4.properties文件可以在一个文件夹下放入类路径上的某个位置。
第一次导入:
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
这个网站上的解决方案对我有用 https://crunchify.com/java-how-to-configure-log4j-logger-property-correctly/ 。 我现在根本看不到 log4j 的警告
我把它放在我放在 src/main/resources 中的 log4j.properties 文件中
# 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 JVM 参数Dlog4j.configuration
一般来说:
添加指出 log4j 配置文件的 JVM 参数。 语法如下:
java [ options ] -jar file.jar [ arguments ]
一个真实的命令行示例如下所示:
java -Dlog4j.configuration=conf/log4j.xml -jar myJarFile.jar myArg1 myArg2
对于 IntelliJ IDE 用户:
1.Run/Debug Configurations
2.Edit configurations...
3.VM options
4.Enter the same value also starting with "-D"
提示:
1.Eclipse IDE 用户会找到一个等效的方法
2.对于运行/调试配置编辑器,很可能在开始时,您的特定可执行文件不存在。 根据您当前正在处理的项目的大小,导航目录以找到它可能会令人不快。 如果您在继续运行/调试配置之前只运行/执行一次文件(单击播放),那么无论执行结果如何,都不会那么麻烦。
3.注意你的工作目录、相对路径和类路径。
对我来说,原因显然不同,错误信息具有误导性。
在我的 build.gradle 中只有这个,它会抱怨日志开头缺少 slf4j,但仍然记录内容,尽管格式很差:
compile 'log4j:log4j:1.2.17'
添加该依赖项会导致我讨论的“找不到附加程序”错误消息,即使我已经在src/main/java/log4j.properties
定义了它们:
compile 'log4j:log4j:1.2.17'
compile 'org.slf4j:slf4j-log4j12:1.7.25'
最后,添加以下依赖项(我只是通过从另一个项目复制它来猜测的)解决了该问题:
compile 'log4j:log4j:1.2.17'
compile 'org.slf4j:slf4j-log4j12:1.7.25'
compile 'commons-logging:commons-logging:1.2'
我不知道为什么,但是有了这个就行了。 对此有何见解?
在java eclipse中,将conf_ref复制到conf文件夹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.