log4j.xml - 警告找不到记录器的附加程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20147433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
log4j.xml - WARN No appenders could be found for logger
提问by nazar_art
I ltried to use log4jloger for my simple project.
我尝试在log4j我的简单项目中使用loger。
But when I run project I cought strainge warning from log4j at console:
但是当我运行项目时,我在控制台从 log4j 发出了应变警告:
log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I couldn't figure out why exactly this happen.
我无法弄清楚为什么会发生这种情况。
I added and downloaded log4j with Maven all should work.
我添加并下载了带有 Maven 的 log4j 都应该可以工作。
I specified apeender, to my mind ok it should work correctly.
But as you can see it isn't.
我指定了 apeender,在我看来它应该可以正常工作。
但正如你所看到的,事实并非如此。
Here is content of my log4j.xml:
这是我的内容log4j.xml:
<!-- Appenders -->
<!-- Loggin into console -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<appender name="file" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="/logs/log.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p (%F:%L:%M) %c{1} - %m%n" />
</layout>
</appender>
<!-- Root logger -->
<root>
<priority value="info" />
<appender-ref ref="file" />
<appender-ref ref="console" />
</root>
<!-- Application Loggers -->
<logger name="com.softserve.edu">
<level value="info" />
</logger>
I missed standart header and footer ( tags).
我错过了标准页眉和页脚(标签)。
One slippery palce is that this file into logsfolder doesn't exist (I guess that log4j will create it by itself).
一个棘手的地方是这个文件logs夹中的文件不存在(我猜 log4j 会自己创建它)。
Here is my project struckture:
这是我的项目结构:


UPDATE:
更新:
I created new folder under src/test/resourcesand moved log4j.xmlinto.
我在下面创建了新文件夹src/test/resources并log4j.xml进入。
Now all are ok with this WARNING but logfile doesn't created.
现在,此警告一切正常,log但未创建文件。
what is wrong with this line <param name="file" value="/logs/log.log" />?
这条线有<param name="file" value="/logs/log.log" />什么问题?
- How to solve this trouble?
- 如何解决这个烦恼?
采纳答案by Nathan Hughes
Your log4j.xml file needs to go in the directory src/main/resources. Log4J is expecting to find it at the root of the classpath.
您的 log4j.xml 文件需要位于 src/main/resources 目录中。Log4J 期望在类路径的根目录中找到它。
The resources directory is the place to put everything that is not Java code, but which you want to go into the classpath. If it's something only tests should use, put it in src/test/resources, otherwise use src/main/resources.
资源目录是放置所有不是 Java 代码但您想要进入类路径的地方。如果它只是测试应该使用的东西,把它放在 src/test/resources 中,否则使用 src/main/resources。
回答by InfernalRapture
I ended up struggling to answer this one myself, so: from the l0g4j site, this is an FAQ, which the error actually points you to.
我最终自己努力回答这个问题,所以:从 l0g4j 站点,这是一个常见问题解答,错误实际上指向您。
http://logging.apache.org/log4j/1.2/faq.html#noconfig
http://logging.apache.org/log4j/1.2/faq.html#noconfig
As you're aware (but for the layman who stumbles across this common error): An Appender is where the output goes. You can have console Appenders, file Appenders, and others which are all spelled out in the directions.
如您所知(但对于偶然发现此常见错误的外行而言):Appender 是输出的所在。您可以拥有控制台 Appenders、文件 Appenders 和其他都在说明中详细说明的内容。
When log4j tells you it has no Appenders registered, it's telling you that the output has nowhere to go. This is why, presumably, your application is silent except for the error.
当 log4j 告诉你它没有注册 Appenders 时,它告诉你输出无处可去。这就是为什么您的应用程序除了错误之外是静默的。
Given that you have, indeed, specified Appenders the error can really be only one of a few things.
鉴于您确实已经指定了 Appenders,错误实际上只是少数事情之一。
If the Appender was somehow malformed, you'd expect to see a Parsing exception, so that's probably not the issue.
如果 Appender 以某种方式格式错误,您会期望看到解析异常,所以这可能不是问题。
IF, however, the log configuration file could not be found AT ALL.....you'd see exactly this problem! More than likely your application simply does not see the configuration file at all and is attempting to resort to defaults.
但是,如果根本找不到日志配置文件.....你会看到这个问题!您的应用程序很可能根本没有看到配置文件,而是试图采用默认值。
How you could get in this state includes:
您如何进入这种状态包括:
- Failure to comply with the maven convention, or supply an alternative configuration. (The solution to which is found in Nathan Hughes' answer).
- Specifying an alternative Maven Configuration that seems correct (enough that maven runs) but fails to do what you think it do.
- Configuring Log4j in-code to look for a file that isn't visible to the application
- 不遵守 Maven 约定,或提供替代配置。(在 Nathan Hughes 的回答中找到了解决方案)。
- 指定一个看起来正确的替代 Maven 配置(足以让 Maven 运行),但无法执行您认为的操作。
- 在代码中配置 Log4j 以查找对应用程序不可见的文件
When you put the file into ./src/main/resources Maven actually bundles the logger.xml INTO the jar. Which puts it on the classpath, which is where the logger is looking by default (obviously the jar is on the classpath).
当您将文件放入 ./src/main/resources 时,Maven 实际上将 logger.xml 捆绑到 jar 中。它将它放在类路径上,这是记录器默认查找的位置(显然 jar 在类路径上)。
However, many users don't want to see the logging configuration packaged insideof their "binary" jar. What's the point of configuring the log-level if you have to either recompile, or essentially do messy in-place editing of a configuration inside of a zip file? So, for many Nathan Hughes' answer is a non-answer, which is why its worth talking about the other scenarios.
但是,许多用户不希望看到打包在他们的“二进制”jar 中的日志配置。如果您必须重新编译或在 zip 文件中对配置进行混乱的就地编辑,那么配置日志级别有什么意义?因此,对于许多 Nathan Hughes 的回答是不回答,这就是为什么它值得谈论其他场景。
If you've packaged your configuration file WITH the jar then the software is not looking in the right place. If you've done any work to move the file yourself, then perhaps you've hidden the log4j.xml file from yourself.
如果您已将配置文件与 jar 一起打包,则该软件的位置不正确。如果您已经完成了自己移动文件的任何工作,那么您可能已经对自己隐藏了 log4j.xml 文件。
However, most likely you packaged the file by using the maven convention ./src/main/config/log4j.xml. If this is the case, then the problem is that your software is not configured to look for it.
但是,您很可能使用 maven 约定 ./src/main/config/log4j.xml 打包文件。如果是这种情况,则问题在于您的软件未配置为查找它。
One method is to specify on the command line when invoking your application
一种方法是在调用应用程序时在命令行上指定
-Dlog4j.configuration=file:conf/log4j.xml
If you're a fan of hard code (I'm not) SOMEWHERE in your code, should be a call
如果你是硬代码的粉丝(我不是)在你的代码中的某个地方,应该是一个电话
For a .properties file:
对于 .properties 文件:
File log4jfile = new File("./conf/log4j.properties");
PropertyConfigurator.configure(log4jfile.getAbsolutePath());
For a .xml file:
对于 .xml 文件:
DOMConfigurator.configure("file:///conf/log4j.xml");
Logger log = Logger.getLogger(YourClass.class);

