Log4j2故障转移无法按预期工作 [英] Log4j2 Failover not working as desired

查看:400
本文介绍了Log4j2故障转移无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello社区!



我在使用Apache的Log4j日志记录功能时遇到问题。



配置文件如下所示:

Hello Community!

I have a problem using Apache's Log4j logging functionality.

The configuration file looks as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="hd.myproject" monitorInterval="120">
    <Appenders>
        <RollingFile name="HDRollingFile" filename="logs/hd.myproject.log"

                     filePattern="logs/hd.myproject.%d{yyyy-MM-dd}.%i.log.gz" ignoreExceptions="false"

                     immediateFlush="true">
            <PatternLayout>
                <pattern>%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %C.%M(%F:%L) - %msg%n%throwable{full}</pattern>
            </PatternLayout>
            <SizeBasedTriggeringPolicy size="8 MB"/>
        </RollingFile>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <JDBC name="HDDatabase" tableName="APPLICATION_LOG">
            <ConnectionFactory class="com.hidensity.data.ConnectionFactory" method="getDatabaseConnection"/>
            <Column name="EVENT_DATE" isEventTimestamp="true" />
            <Column name="LEVEL" pattern="%level" />
            <Column name="LOGGER" pattern="%logger" />
            <Column name="FILENAME" pattern="%file" />
            <Column name="LINE" pattern="%line" />
            <Column name="MESSAGE" pattern="%message" />
            <Column name="THROWABLE" pattern="%ex{full}" />
        </JDBC>
        <Failover name="HDFailover" primary="HDDatabase" ignoreExceptions="false">
            <Failovers>
                <AppenderRef ref="HDRollingFile" level="trace"/>
                <AppenderRef ref="Console"/>
            </Failovers>
        </Failover>
    </Appenders>
    <Loggers>
        <Logger name="com.hidensity" level="trace" additivity="false">
            <AppenderRef ref="HDFailover"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>





如图所示,主记录器应将信息写入数据库表。这很好。

当记录器无法写入数据库时​​会出现问题。

据我了解故障转移记录器,它应该使用HDRollingFile记录器这个情况。但是在程序结束后,日志文件仍然是空的。

使用配置状态=警告级别,我得到以下内容错误: ERROR appender故障转移没有与元素故障转移相匹配的参数

但是这段代码与Log4j2文档中的代码完全相同。

结果是,数据库无法访问的例外情况被打印到控制台,但我没有在我的程序代码中生成我的日志信息。



有没有人知道这里发生了什么以及如何修复它?



提前谢谢。 :)



As seen, the primary logger shall write the information to a database table. This works fine.
The problem occurs when the logger cannot write to the database.
As I understand the "Failover" logger, it should use the "HDRollingFile" logger in this situation. But after the program has ended, the Log-file is still empty.
With the Configuration status="warn" level, I get the following error: ERROR appender Failover has no parameter that matches element Failovers
But this code is exactly like the code in the Log4j2 documentation.
The result is, the exceptions for the database not being accessible, are printed to the console, but none of my logging information I am generating in my program code.

Does anybody have a clue what is going on here and how to fix it?

Thanks in advance. :)

推荐答案

我有完全相同的问题,我相信我们并不孤单。我看到非常奇怪的行为,其中我的集成测试使用相同的类路径(除了额外的测试库),不会抛出此错误,因此故障转移工作。所以,我的集成测试通过但是当我从main()方法运行我的代码时,log4j2配置抛出了这个蹩脚的错误。



我试过重新订购我的元素,但根据文档,你列出它们的方式是正确的。如果我不得不猜测,配置管理器中存在竞争条件,它尝试在实例化之前初始化故障转移appender。为什么在我的集成测试中不会发生这种情况,我不知道。这是一个实时的追踪者,这种行为对我的申请至关重要。
I have the exact same problem, and I believe we are not alone. I see very strange behaviour, wherein my integration test, which uses an identical classpath (except the additional testing libs), does NOT throw this error and so failover works. So, my integration tests pass but when I run my code from my main() method, the log4j2 config throws this lame error.

I've tried re-ordering my elements but according to the documentation, the way you have them listed is correct. If I had to guess, there is a race condition in the config manager that tries to initialize the failover appender before it's instantiated. Why this doesn't happen in my integration tests, I don't know. It's a real time sucker to track down and this behaviour is critical to my application.


这篇关于Log4j2故障转移无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆