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 Gary Gregory <ga...@gmail.com> on 2016/09/01 21:18:46 UTC

Adding and removing Appenders.

Hi,

I think we have some kind of discrepancy in
org.apache.logging.log4j.core.Logger between:

    /**
     * This method is not exposed through the public API and is used
primarily for unit testing.
     *
     * @param appender The Appender to add to the Logger.
     */
    public void addAppender(final Appender appender) {
        privateConfig.config.addLoggerAppender(this, appender);
    }

and:

    /**
     * This method is not exposed through the public API and is used
primarily for unit testing.
     *
     * @param appender The Appender to remove from the Logger.
     */
    public void removeAppender(final Appender appender) {
        privateConfig.loggerConfig.removeAppender(appender.getName());
    }

Because, when in individual test methods in

org.apache.logging.log4j.core.appender.SocketAppenderTest

We do calls like

root.addAppender(newAppender)

and in the @After teardown method we do:

root.removeAppender(appender);

But that does not remove the appender from the 'appenders' map
in org.apache.logging.log4j.core.config.AbstractConfiguration. So the next
time you try to add a fresh appender in the next test, the code in
addLoggerAppender calls putIfAbsent and since it is still there, the old
closed appender is used and tests fail.

I have some new tests locally that are not committed that show this problem.

To see it you can simply duplicate the method testTcpAppender() as
testTcpAppender2() and boom!

Gary


-- 
E-Mail: garydgregory@gmail.com | ggregory@apache.org
Java Persistence with Hibernate, Second Edition
<http://www.manning.com/bauer3/>
JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
Spring Batch in Action <http://www.manning.com/templier/>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory

Re: Adding and removing Appenders.

Posted by Ralph Goers <ra...@dslextreme.com>.
Not at this instant. I am actually still working. I should have time this weekend.

Ralph

> On Sep 1, 2016, at 2:38 PM, Gary Gregory <ga...@gmail.com> wrote:
> 
> I need some help to get that fixed. It seems that there are a lot of subtleties in that part of the code. Can you help?
> 
> Thank you,
> Gary
> 
> On Thu, Sep 1, 2016 at 2:32 PM, Ralph Goers <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
> Yeah, that looks wrong.
> 
> Ralph
> 
>> On Sep 1, 2016, at 2:18 PM, Gary Gregory <garydgregory@gmail.com <ma...@gmail.com>> wrote:
>> 
>> Hi,
>> 
>> I think we have some kind of discrepancy in org.apache.logging.log4j.core.Logger between:
>> 
>>     /**
>>      * This method is not exposed through the public API and is used primarily for unit testing.
>>      *
>>      * @param appender The Appender to add to the Logger.
>>      */
>>     public void addAppender(final Appender appender) {
>>         privateConfig.config.addLoggerAppender(this, appender);
>>     }
>> 
>> and:
>> 
>>     /**
>>      * This method is not exposed through the public API and is used primarily for unit testing.
>>      *
>>      * @param appender The Appender to remove from the Logger.
>>      */
>>     public void removeAppender(final Appender appender) {
>>         privateConfig.loggerConfig.removeAppender(appender.getName());
>>     }
>> 
>> Because, when in individual test methods in 
>> 
>> org.apache.logging.log4j.core.appender.SocketAppenderTest
>> 
>> We do calls like 
>> 
>> root.addAppender(newAppender)
>> 
>> and in the @After teardown method we do:
>> 
>> root.removeAppender(appender);
>> 
>> But that does not remove the appender from the 'appenders' map in org.apache.logging.log4j.core.config.AbstractConfiguration. So the next time you try to add a fresh appender in the next test, the code in addLoggerAppender calls putIfAbsent and since it is still there, the old closed appender is used and tests fail.
>> 
>> I have some new tests locally that are not committed that show this problem.
>> 
>> To see it you can simply duplicate the method testTcpAppender() as testTcpAppender2() and boom!
>> 
>> Gary
>> 
>> 
>> -- 
>> E-Mail: garydgregory@gmail.com <ma...@gmail.com> | ggregory@apache.org  <ma...@apache.org>
>> Java Persistence with Hibernate, Second Edition <http://www.manning.com/bauer3/>
>> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
>> Spring Batch in Action <http://www.manning.com/templier/>
>> Blog: http://garygregory.wordpress.com <http://garygregory.wordpress.com/> 
>> Home: http://garygregory.com/ <http://garygregory.com/>
>> Tweet! http://twitter.com/GaryGregory <http://twitter.com/GaryGregory>
> 
> 
> 
> -- 
> E-Mail: garydgregory@gmail.com <ma...@gmail.com> | ggregory@apache.org  <ma...@apache.org>
> Java Persistence with Hibernate, Second Edition <http://www.manning.com/bauer3/>
> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> Spring Batch in Action <http://www.manning.com/templier/>
> Blog: http://garygregory.wordpress.com <http://garygregory.wordpress.com/> 
> Home: http://garygregory.com/ <http://garygregory.com/>
> Tweet! http://twitter.com/GaryGregory <http://twitter.com/GaryGregory>

Re: Adding and removing Appenders.

Posted by Gary Gregory <ga...@gmail.com>.
I need some help to get that fixed. It seems that there are a lot of
subtleties in that part of the code. Can you help?

Thank you,
Gary

On Thu, Sep 1, 2016 at 2:32 PM, Ralph Goers <ra...@dslextreme.com>
wrote:

> Yeah, that looks wrong.
>
> Ralph
>
> On Sep 1, 2016, at 2:18 PM, Gary Gregory <ga...@gmail.com> wrote:
>
> Hi,
>
> I think we have some kind of discrepancy in org.apache.logging.log4j.core.
> Logger between:
>
>     /**
>      * This method is not exposed through the public API and is used
> primarily for unit testing.
>      *
>      * @param appender The Appender to add to the Logger.
>      */
>     public void addAppender(final Appender appender) {
>         privateConfig.config.addLoggerAppender(this, appender);
>     }
>
> and:
>
>     /**
>      * This method is not exposed through the public API and is used
> primarily for unit testing.
>      *
>      * @param appender The Appender to remove from the Logger.
>      */
>     public void removeAppender(final Appender appender) {
>         privateConfig.loggerConfig.removeAppender(appender.getName());
>     }
>
> Because, when in individual test methods in
>
> org.apache.logging.log4j.core.appender.SocketAppenderTest
>
> We do calls like
>
> root.addAppender(newAppender)
>
> and in the @After teardown method we do:
>
> root.removeAppender(appender);
>
> But that does not remove the appender from the 'appenders' map
> in org.apache.logging.log4j.core.config.AbstractConfiguration. So the
> next time you try to add a fresh appender in the next test, the code in
> addLoggerAppender calls putIfAbsent and since it is still there, the old
> closed appender is used and tests fail.
>
> I have some new tests locally that are not committed that show this
> problem.
>
> To see it you can simply duplicate the method testTcpAppender() as
> testTcpAppender2() and boom!
>
> Gary
>
>
> --
> E-Mail: garydgregory@gmail.com | ggregory@apache.org
> Java Persistence with Hibernate, Second Edition
> <http://www.manning.com/bauer3/>
> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> Spring Batch in Action <http://www.manning.com/templier/>
> Blog: http://garygregory.wordpress.com
> Home: http://garygregory.com/
> Tweet! http://twitter.com/GaryGregory
>
>
>


-- 
E-Mail: garydgregory@gmail.com | ggregory@apache.org
Java Persistence with Hibernate, Second Edition
<http://www.manning.com/bauer3/>
JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
Spring Batch in Action <http://www.manning.com/templier/>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory

Re: Adding and removing Appenders.

Posted by Ralph Goers <ra...@dslextreme.com>.
Yeah, that looks wrong.

Ralph

> On Sep 1, 2016, at 2:18 PM, Gary Gregory <ga...@gmail.com> wrote:
> 
> Hi,
> 
> I think we have some kind of discrepancy in org.apache.logging.log4j.core.Logger between:
> 
>     /**
>      * This method is not exposed through the public API and is used primarily for unit testing.
>      *
>      * @param appender The Appender to add to the Logger.
>      */
>     public void addAppender(final Appender appender) {
>         privateConfig.config.addLoggerAppender(this, appender);
>     }
> 
> and:
> 
>     /**
>      * This method is not exposed through the public API and is used primarily for unit testing.
>      *
>      * @param appender The Appender to remove from the Logger.
>      */
>     public void removeAppender(final Appender appender) {
>         privateConfig.loggerConfig.removeAppender(appender.getName());
>     }
> 
> Because, when in individual test methods in 
> 
> org.apache.logging.log4j.core.appender.SocketAppenderTest
> 
> We do calls like 
> 
> root.addAppender(newAppender)
> 
> and in the @After teardown method we do:
> 
> root.removeAppender(appender);
> 
> But that does not remove the appender from the 'appenders' map in org.apache.logging.log4j.core.config.AbstractConfiguration. So the next time you try to add a fresh appender in the next test, the code in addLoggerAppender calls putIfAbsent and since it is still there, the old closed appender is used and tests fail.
> 
> I have some new tests locally that are not committed that show this problem.
> 
> To see it you can simply duplicate the method testTcpAppender() as testTcpAppender2() and boom!
> 
> Gary
> 
> 
> -- 
> E-Mail: garydgregory@gmail.com <ma...@gmail.com> | ggregory@apache.org  <ma...@apache.org>
> Java Persistence with Hibernate, Second Edition <http://www.manning.com/bauer3/>
> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> Spring Batch in Action <http://www.manning.com/templier/>
> Blog: http://garygregory.wordpress.com <http://garygregory.wordpress.com/> 
> Home: http://garygregory.com/ <http://garygregory.com/>
> Tweet! http://twitter.com/GaryGregory <http://twitter.com/GaryGregory>