qos-ch / logback

The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch
Other
3.02k stars 1.28k forks source link

appender can't be added to AsyncAppender after being detached. #725

Open wsliliang opened 1 year ago

wsliliang commented 1 year ago

1. behavior

appender can't be added to AsyncAppender after being detached.

2. reproduce and test case

add following test in AsyncAppenderBaseTest

    @Test
    @Timeout(value=2, unit = TimeUnit.SECONDS)
    public void appenderShouldBeAddedSuccessfullyAfterDetachedByReference() {
        asyncAppenderBase.addAppender(listAppender);
        asyncAppenderBase.start();
        asyncAppenderBase.doAppend(0);
        asyncAppenderBase.detachAppender(listAppender);
        asyncAppenderBase.addAppender(listAppender);
        asyncAppenderBase.doAppend(0);
        asyncAppenderBase.stop();
        verify(listAppender, 2);
    }

3. cause

the value of property "appenderCount" does't decrease in method "detachAppender", the value is 1 after first addAppender, and still 1 after detachAppender, so when addAppender again, it goes into "else".

  public void addAppender(Appender<E> newAppender) {
        if (appenderCount == 0) {
            appenderCount++;
            addInfo("Attaching appender named [" + newAppender.getName() + "] to AsyncAppender.");
            aai.addAppender(newAppender);
        } else {
            addWarn("One and only one appender may be attached to AsyncAppender.");
            addWarn("Ignoring additional appender named [" + newAppender.getName() + "]");
        }
    }