Click here to Skip to main content
15,886,045 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Community!

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

The configuration file looks as follows:
XML
<?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>


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

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900