java Log4j2 的 FailoverAppender 错误:appender Failover 没有与元素 Failovers 匹配的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28276619/
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
Log4j2's FailoverAppender Error: appender Failover has no parameter that matches element Failovers
提问by Deses
When I compile my spring 3.2.9 web application using log4j 2.1, this error appears in the console:
当我使用 log4j 2.1 编译 spring 3.2.9 Web 应用程序时,控制台中出现此错误:
2015-02-02 12:08:25,213 ERROR appender Failover has no parameter that matches element Failovers
What I understand is that the element "Failovers" does not exist inside the element "Failover", right? Why would this happen? I don't see whats wrong since I have the same configuration as the log4j2 manual.
我的理解是元素“故障转移”中不存在“故障转移”元素,对吗?为什么会发生这种情况?我看不出有什么问题,因为我的配置与 log4j2 手册相同。
I have this configuration in my log4j2.xml:
我的 log4j2.xml 中有这个配置:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration name="vcr-log4j2-config" status="debug">
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout>
<Pattern>[%d{ISO8601}] %c [%C{1}] - %p: %m%n</Pattern>
</PatternLayout>
</Console>
<Syslog name="SYS_LOG" host="test_server.com" port="514"
protocol="UDP" facility="LOCAL7">
</Syslog>
<RollingFile name="backupApp"
fileName="C:/backup.log"
filePattern="C:/backup-%d{yyyy-MM-dd_HH-mm}.log.gz">
<PatternLayout>
<Pattern>[%d{ISO8601}] [%c] - %p: %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
</RollingFile>
<Failover name="FAILOVER" primary="SYS_LOG">
<Failovers>
<AppenderRef ref="backupApp"/>
</Failovers>
</Failover>
</Appenders>
<Loggers>
<Logger name="com.test.util.CustomLogger" level="info" additivity="false">
<AppenderRef ref="SYS_LOG" />
<AppenderRef ref="STDOUT" />
</Logger>
<Logger name="STDOUT" level="info" additivity="false">
<AppenderRef ref="STDOUT" />
</Logger>
<Root level="info">
<AppenderRef ref="STDOUT" />
<AppenderRef ref="LOG" />
</Root>
<Root level="error">
<AppenderRef ref="FAILOVER"/>
</Root>
</Loggers>
</Configuration>
Thanks for the help.
谢谢您的帮助。
回答by Robin Coe
What I have seen while debugging is the PluginBuilder#verifyNodeChildrenUsed() method is called to, you would expect, verify that the child elements of the Node are correct. As I describe below, this method name does not match the behaviour.
我在调试时看到的是调用 PluginBuilder#verifyNodeChildrenUsed() 方法来验证节点的子元素是否正确。正如我在下面描述的,这个方法名称与行为不匹配。
In the case of a Failover appender, the plugin type is: (btw, the extra "==" at "isDeferChildren" is just in the toString() implementation and does not affect the test.)
在故障转移附加程序的情况下,插件类型是:(顺便说一句,“isDeferChildren”处的额外“==”只是在 toString() 实现中,不会影响测试。)
PluginType [pluginClass=class org.apache.logging.log4j.core.appender.FailoversPlugin, key=failovers, elementName=failovers, isObjectPrintable=false, isDeferChildren==false, category=core]
The implementation of this method:
该方法的实现:
private void verifyNodeChildrenUsed() {
final List<Node> children = node.getChildren();
if (!(pluginType.isDeferChildren() || children.isEmpty())) {
for (final Node child : children) {
final String nodeType = node.getType().getElementName();
final String start = nodeType.equals(node.getName()) ? node.getName() : nodeType + ' ' + node.getName();
LOGGER.error("{} has no parameter that matches element {}", start, child.getName());
}
}
}
When the nodeType is the FailoversPlugin class (see above), the node type is "appender" but the name is "Failovers". So the test of equality produces the string "appender Failovers".
当 nodeType 为 FailoversPlugin 类(见上文)时,节点类型为“appender”但名称为“Failovers”。因此相等性测试产生字符串“appender Failovers”。
What I haven't dug into fully is WHY this method is called such that if the node is not empty and the property of deferring is false, the error message MUST be displayed. It seems to me that the logic here is dependent on something up-the-stack and tracing through this is cumbersome.
我没有深入研究的是为什么要调用此方法,以便如果节点不为空且延迟属性为 false,则必须显示错误消息。在我看来,这里的逻辑依赖于堆栈上的东西,并且跟踪它很麻烦。
What I'd really like to do is ask the developer responsible what's the intention, because it's not clear to me that the behaviour is correct. Well, obviously that's true in this case. :S
我真正想做的是询问负责的开发人员的意图是什么,因为我不清楚这种行为是否正确。嗯,显然在这种情况下确实如此。:S
回答by user31601
This isa bug in log4j. I've pointed out the error in the issue linked to by @Deses. For the time-being, here's a workaround:
这是log4j 中的一个错误。我已经指出了@Deses 链接的问题中的错误。暂时,这里有一个解决方法:
/**
* Avoids a bug in log4j, whereby plugins that produce an array (e.g. {@linkplain FailoversPlugin})
* cause an error to be logged.
* <p>
* To use, instead of this:
* <pre>
* <Failover name="MyAppender" primary="xxx">
* <Failovers>
* <AppenderRef ref="fallback1"/>
* <AppenderRef ref="fallback2"/>
* </Failovers>
* </Failover>
* </pre>
* do this:
* <pre>
* <Failover name="MyAppender" primary="xxx">
* <Fallback ref="fallback1"/>
* <Fallback ref="fallback2"/>
* </Failover>
* </pre>
*/
@Plugin(name="Fallback", category=Node.CATEGORY, elementType="Failovers")
public class Fallback {
private Fallback(){}
@PluginFactory
public static String createFallbackRef(@PluginAttribute("ref") @Required String ref) {
return ref;
}
}
Just stick this on your compile and runtime classpaths, and make sure annotation processing is enabled.
只需将其粘贴在您的编译和运行时类路径上,并确保启用了注释处理。
回答by Chandru
I had a similar issue and i had commented the failover tags in Log4j xml and all my logs are getting created now
我有一个类似的问题,我在 Log4j xml 中评论了故障转移标签,现在我的所有日志都被创建了
回答by Deses
For anyone that runs into this problem, it's a bug in log4j2.
对于遇到此问题的任何人来说,这是 log4j2 中的一个错误。
I ran into this JIRA issueand found this commentary in its xml config file:
我遇到了这个 JIRA 问题,并在它的 xml 配置文件中找到了这个评论:
<!-- set status below to FATAL to suppress a bug in log4j2: "ERROR appender Failover has no parameter that matches element Failovers" -->
<!-- other working value for status is "warn" - -->
Not much else to do.
没什么可做的。
回答by tiger
in my case, you should add ignoreExceptions
filed on both primary and failovers.
就我而言,您应该ignoreExceptions
在主要和故障转移上添加归档。
`<Console name="Console-Appender" target="SYSTEM_OUT" ignoreExceptions="false">
<PatternLayout>
<pattern>${log-pattern}</pattern>
</PatternLayout>
</Console>`