[
https://issues.apache.org/jira/browse/LOG4J2-291?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13705559#comment-13705559
]
Michael Kloster commented on LOG4J2-291:
----------------------------------------
It seems to the the issue goes deeper than just the value of the
handleExceptions flag. In the current trunk, the JDBCDatabaseManager
writeInternal() method catches SQLExceptions, logs them, and does not throw
them as AppenderRuntimeException. This code starts in JDBCDatabaseManager on
line 117.
{code:title=JDBCDatabaseManager.java|borderStyle=solid}
117: } catch (final SQLException e) {
118: LOGGER.error("Failed to insert record for log event in manager
[{}].", this.getName(), e);
119: } finally {
{code}
This means any exception in writing the log to the database gets trapped. I'm
new to the Log4J 2 codebase, but it seems to me there should be an additional
line after 118 to the effect of:
{code:title=JDBCDatabaseManager.java|borderStyle=solid}
} catch (final SQLException e) {
LOGGER.error("Failed to insert record for log event in manager
[{}].", this.getName(), e);
throw new
org.apache.logging.log4j.core.appender.AppenderRuntimeException(e);
} finally {
{code}
After making the change to JDBCAppender per Thomas N. comment and adding the
throw above, I am able to get the failover logs appearing in the log file. The
use case I used for testing a failure is to shutdown the database server after
the application is running, thus simulating a database failure or connectivity
problem.
Scanning through the rest of JDBCDatabaseManager seems to have other places
where the log is not written to the database due to some error condition, but
no exception is raised. Line 87 has a check for a null or closed connection,
and simply returns after logging if the connection is not available.
{code:title=JDBCDatabaseManager.java|borderStyle=solid}
87: if (!this.isConnected() || this.connection == null ||
this.connection.isClosed()) {
LOGGER.error("Cannot write logging event; manager [{}] not
connected to the database.", this.getName());
return;
}
{code}
Unless I am missing something, it appears to me that the JDBCAppender is not
written with failover in mind.
> Failover appender doesn't fail over on JDBC appender error
> ----------------------------------------------------------
>
> Key: LOG4J2-291
> URL: https://issues.apache.org/jira/browse/LOG4J2-291
> Project: Log4j 2
> Issue Type: Bug
> Components: Appenders
> Affects Versions: 2.0-beta7
> Environment: Java version 1.6_045 and Oracle 11g 11.2.0.3.0 database
> on 64-bit machine. It's running within eclipse but I don't think that should
> make a difference.
> Reporter: Asaf Erlich
> Priority: Minor
>
> When I configure a Failover appender using the jdbc appender as primary and
> console/file as secondary if I get a database error It prints something to
> System.err, but not to the file/console as expected.
> How to recreate:
> Make sure a jdbc appender works first.
> Prepare a configuration similar to the one below:
> <?xml version="1.0" encoding="UTF-8"?>
> <configuration status="WARN">
> <appenders>
> <Jdbc name="HubManagerDatabaseLog" tablename="HUB_MANAGER_LOG">
> <ConnectionFactory
> class="com.somecompany.server.dal.DalCommon"
> method="getDatabaseInstanceConnection"/>
> <Column name="LOG_DATE" isEventTimestamp="true" />
> <Column name="LOG_THREAD" pattern="%thread" />
> <Column name="LOG_LEVEL" pattern="%level" />
> <Column name="LOG_CLASS" pattern="%logger" />
> <Column name="LOG_METHOD" pattern="%method" />
> <Column name="LOG_MESSAGE" pattern="%message" />
> <Column name="LOG_EXCEPTION" pattern="%exception" />
> </Jdbc>
> <FastRollingFile name="HubManagerFileLog"
> filename="logs/HubManager.log" filePattern="logs/HubManager-%d{COMPACT}.log">
> <PatternLayout pattern="%d [%thread] %-5level
> %logger.%method - %message %exception%n" />
> <Policies>
> <SizeBasedTriggeringPolicy size="5MB"/>
> </Policies>
> <!-- <DefaultRolloverStrategy max="50"/> -->
> </FastRollingFile>
> <Console name="Console" target="SYSTEM_OUT">
> <PatternLayout pattern="%d [%thread] %-5level
> %logger.%method - %message %exception%n" />
> </Console>
> <Failover name="PrimaryDatabaseLoggingIfFailGoToFile"
> primary="HubManagerDatabaseLog" suppressExceptions="false">
> <Failovers>
> <appender-ref ref="HubManagerFileLog"/>
> </Failovers>
> </Failover>
> </appenders>
>
> <loggers>
> <logger name="com.exzac" level="DEBUG">
> <appender-ref
> ref="PrimaryDatabaseLoggingIfFailGoToFile"/>
> <appender-ref ref="Console"/>
> </logger>
> <root level="DEBUG">
> </root>
> </loggers>
> </configuration>
> Run the following in a test java class that correctly has the above xml
> configuration on the build path:
> import org.apache.logging.log4j.LogManager;
> import org.apache.logging.log4j.Logger;
> public class TestFailOverAppender {
> private static final Logger LOGGER =
> LogManager.getLogger(TestFailOverAppender.class);
> public static void main(final String[] args) {
> final String shortString = "FailOver Short message that should
> go to database";
> LOGGER.info(shortString);
> final StringBuilder longString = new StringBuilder(shortString);
> while (longString.length() < 4000) {
> longString.append(shortString);
> }
> LOGGER.info(longString.toString());
> LOGGER.info("FailOver short string after message");
> }
> }
> I have tried setting suppressExceptions as false/true. I have tried
> replacing the fileappender (which works normally) with just console instead
> and saw the same result. This may or may not be related to another JIRA
> issue I found, LOG4J2-126.
> If there is simply something wrong with my configuration please let me know.
> I tried following the documentation as much as possible. Thank you.
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]