[ 
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 7:58 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. 

The biggest drawback is that any appender which failed at start-up appears to 
be kicked out of the failover list - which means if our network share was down 
at start-up we never log to it without a restart. I think this is where we need 
to use the timer and occasionally try to pick-up the 'early' appenders that 
failed at start-up ... that is time consumptive enough that I wouldn't want to 
do it too often.
<note: We are currently looking at 'touching' the configuration file once we 
get the share working and letting the MonitorInterval reload reconnect to the 
share>

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

{code}
    @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);
        }
    }
    **********/
{code}



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. 

The biggest drawback is that any appender which failed at start-up appears to 
be kicked out of the failover list - which means if our network share was down 
at start-up we never log to it without a restart. I think this is where we need 
to use the timer and occasionally try to pick-up the 'early' appenders that 
failed at start-up ... that is time consumptive enough that I wouldn't want to 
do it too often.

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

{code}
    @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);
        }
    }
    **********/
{code}


> 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: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to