Log4j2'的FailoverAppender错误:appender Failover没有匹配元素Failovers的参数[英] Log4j2's FailoverAppender Error: appender Failover has no parameter that matches element Failovers

本文是小编为大家收集整理的关于Log4j2'的FailoverAppender错误:appender Failover没有匹配元素Failovers的参数的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

当我使用log4j 2.1编译弹簧3.2.9 Web应用程序时,此错误将出现在控制台中:

2015-02-02 12:08:25,213 ERROR appender Failover has no parameter that matches element Failovers

我了解的是,元素"故障转移"不存在元素"故障转移",对吗?为什么会发生这种情况?我看不到怎么了,因为我有与与log4j2手册.

我在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>

感谢您的帮助.

推荐答案

我在调试时看到的是插件builder#verifynodechildrensed()方法,您会期望验证节点的子元素是否正确.正如我在下面描述的那样,此方法名称与行为不符.

在故障转移appender的情况下,插件类型为:(btw,额外的" ==" at" isdeferchildren"仅在tostring()实现中,并且不影响测试.)

)

PluginType [pluginClass=class org.apache.logging.log4j.core.appender.FailoversPlugin, key=failovers, elementName=failovers, isObjectPrintable=false, isDeferChildren==false, category=core]

这种方法的实现:

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());
        }
   }
}

当结节类型是故障转移Splugin类(请参见上文)时,节点类型为" appender",但名称为"故障转移".因此,平等测试产生字符串" Appender故障转移".

我尚未完全挖掘的原因是为什么将此方法称为为什么节点不是空的,并且延期的属性为false,则必须显示错误消息.在我看来,这里的逻辑取决于堆栈的事物,并在此过程中追踪很麻烦.

我真正想做的是询问开发人员负责是什么意图,因为我不清楚行为是正确的.好吧,显然在这种情况下是如此. :S

其他推荐答案

此是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>
 * &lt;Failover name="MyAppender" primary="xxx">
 *   &lt;Failovers>
 *     &lt;AppenderRef ref="fallback1"/>
 *     &lt;AppenderRef ref="fallback2"/>
 *   &lt;/Failovers>
 * &lt;/Failover>
 * </pre>
 * do this:
 * <pre>
 * &lt;Failover name="MyAppender" primary="xxx">
 *   &lt;Fallback ref="fallback1"/>
 *   &lt;Fallback ref="fallback2"/>
 * &lt;/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;
    }

}

只需将其粘在您的编译和运行时类路径上,并确保启用注释处理.

其他推荐答案

对于任何遇到此问题的人,它是log4j2中的一个错误.

我遇到了此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" -  -->

没有其他要做的.

本文地址:https://www.itbaoku.cn/post/1575209.html

问题描述

When I compile my spring 3.2.9 web application using log4j 2.1, this error appears in the console:

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.

I have this configuration in my 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.

推荐答案

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.

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.)

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".

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.

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

其他推荐答案

This is a bug in log4j. I've pointed out the error in the issue linked to by @Deses. For the time-being, here's a workaround:

/**
 * 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>
 * &lt;Failover name="MyAppender" primary="xxx">
 *   &lt;Failovers>
 *     &lt;AppenderRef ref="fallback1"/>
 *     &lt;AppenderRef ref="fallback2"/>
 *   &lt;/Failovers>
 * &lt;/Failover>
 * </pre>
 * do this:
 * <pre>
 * &lt;Failover name="MyAppender" primary="xxx">
 *   &lt;Fallback ref="fallback1"/>
 *   &lt;Fallback ref="fallback2"/>
 * &lt;/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.

其他推荐答案

For anyone that runs into this problem, it's a bug in log4j2.

I ran into this JIRA issue and found this commentary in its xml config file:

<!-- 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.