You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by "Ronald C Albury (JIRA)" <ji...@apache.org> on 2015/03/13 17:30:39 UTC

[jira] [Comment Edited] (LOG4J2-975) Failover Appender does not start if primary appender fails

    [ https://issues.apache.org/jira/browse/LOG4J2-975?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14360575#comment-14360575 ] 

Ronald C Albury edited comment on LOG4J2-975 at 3/13/15 4:30 PM:
-----------------------------------------------------------------

I have slapped together a temporary fix for our site ... I am not suggesting this is the final solution. There is more cleaning-up and elimination of dead code that can be done, but my goal was to change as few lines of code as possible.

Essentially I totally ignore the primary appender and just run with a list of failover appenders.

<pre>
    @Override
    public void start() {
        final Map<String, Appender> map = config.getAppenders();
        int errors = 0;

        /**********
        if (map.containsKey(primaryRef)) {
            primary = new AppenderControl(map.get(primaryRef), null, null);
        } else {
            LOGGER.error("Unable to locate primary Appender " + primaryRef);
            ++errors;
        }
        **********/

        for (final String name : failovers) {
            if (map.containsKey(name)) {
                failoverAppenders.add(new AppenderControl(map.get(name), null, null));
            } else {
                LOGGER.error("Failover appender " + name + " is not configured");
            }
        }
        if (failoverAppenders.isEmpty()) {
            LOGGER.error("No failover appenders are available");
            ++errors;
        }
        if (errors == 0) {
            super.start();
        }
    }

    /**
     * Handle the Log event.
     * @param event The LogEvent.
     */
    @Override
    public void append(final LogEvent event) {
        if (!isStarted()) {
            error("FailoverAppender " + getName() + " did not start successfully");
            return;
        }

        /**********
        final long localCheckMillis = nextCheckMillis;
        if (localCheckMillis == 0 || System.currentTimeMillis() > localCheckMillis) {
            callAppender(event);
        } else {
            failover(event, null);
        }
        **********/

        failover(event, null);
    }

    /**********
    private void callAppender(final LogEvent event) {
        try {
            primary.callAppender(event);
            nextCheckMillis = 0;
        } catch (final Exception ex) {
            nextCheckMillis = System.currentTimeMillis() + intervalMillis;
            failover(event, ex);
        }
    }
    **********/
</pre>



was (Author: eialbur):
I have slapped together a temporary fix for our site ... I am not suggesting this is the final solution. There is more cleaning-up and elimination of dead code that can be done, but my goal was to change as few lines of code as possible.

Essentially I totally ignore the primary appender and just run with a list of failover appenders.

    @Override
    public void start() {
        final Map<String, Appender> map = config.getAppenders();
        int errors = 0;
        /*
        if (map.containsKey(primaryRef)) {
            primary = new AppenderControl(map.get(primaryRef), null, null);
        } else {
            LOGGER.error("Unable to locate primary Appender " + primaryRef);
            ++errors;
        }
        */
        for (final String name : failovers) {
            if (map.containsKey(name)) {
                failoverAppenders.add(new AppenderControl(map.get(name), null, null));
            } else {
                LOGGER.error("Failover appender " + name + " is not configured");
            }
        }
        if (failoverAppenders.isEmpty()) {
            LOGGER.error("No failover appenders are available");
            ++errors;
        }
        if (errors == 0) {
            super.start();
        }
    }

    /**
     * Handle the Log event.
     * @param event The LogEvent.
     */
    @Override
    public void append(final LogEvent event) {
        if (!isStarted()) {
            error("FailoverAppender " + getName() + " did not start successfully");
            return;
        }
        /*
        final long localCheckMillis = nextCheckMillis;
        if (localCheckMillis == 0 || System.currentTimeMillis() > localCheckMillis) {
            callAppender(event);
        } else {
            failover(event, null);
        }
        */
        failover(event, null);
    }

    /*
    private void callAppender(final LogEvent event) {
        try {
            primary.callAppender(event);
            nextCheckMillis = 0;
        } catch (final Exception ex) {
            nextCheckMillis = System.currentTimeMillis() + intervalMillis;
            failover(event, ex);
        }
    }
    */


> Failover Appender does not start if primary appender fails
> ----------------------------------------------------------
>
>                 Key: LOG4J2-975
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-975
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Appenders
>    Affects Versions: 2.2
>         Environment: Windows-7
>            Reporter: Ronald C Albury
>
> We are supposed to log to a network share - but sometimes it is not available. I created a Failover appender with a RollingFile appender to the network share as the primary, and a RollingFile appender to a local drive as a failover. If the network share is not available when my application starts, the Failover appender fails in its entirety and nothing is logged anywhere.
>       <Failover name="MyFile" primary="ShareFile">
>          <Failovers>
>             <AppenderRef ref="LocalFile" />
>             <AppenderRef ref="MyConsole" />
>          </Failovers>
>       </Failover>
> 2015-03-12 12:06:49,717 ERROR Attempted to append to non-started appender MyFile
> 2015-03-12 12:06:49,717 ERROR FailoverAppender MyFile did not start successfully
> 2015-03-12 12:06:49,717 ERROR Attempted to append to non-started appender MyFile
> 2015-03-12 12:06:49,717 ERROR FailoverAppender MyFile did not start successfully



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org