Anyway, here is the message I originally posted to the user group:


-------- Doorgestuurd bericht --------
Onderwerp:      Builder for FileAppender
Datum:  Wed, 12 Aug 2015 18:42:04 +0200
Van:    Xen <x...@dds.nl>
Aan:    log4j-u...@logging.apache.org



Hi,

I decided it would be fun and educational to try to write a builder for the FileAppender, seeing as it is not there. Perhaps this ought to go to the dev list, but that is rather high volume :-/.

And maybe you want this as a Jira ticket, but that is rather odd communicating I feel.

The FileAppender has an issue with creating a builder:

- the create method takes only Strings and a few objects.
- the real constructor takes regular parameters.
- this is equivalent for ConsoleAppender.Builder
- the create method performs sanity checks and logs error messages and returns if conditions are not met with a null. - the same is true for ConsoleAppender.Builder, but it just skips those and calls the constructor directlty. - were I to create a builder I would follow the example of the ConsoleAppender and not replicate/duplicate the code that does all the checks however this can result in erroneous application.

- ideally you would refactor the sanity checks into a different method (it operates on the parsed values (Strings) anyway) since you now have two (equivalent) ways of constructing the object
- I will do so for good day's fortune's sake anyway.

- the sanity checks may change one of the parameters, a boolean. To do it with a central method would require e.g.*org.apache.commons.lang.mutable.MutableBoolean* to be passed along (or an equivalent, could be an inner class) and I don't know how hideous you'd consider that.

- essentially the create method *could* use the builder, but if the builder used the create, it would have to convert all the booleans back to string, where they are converted to boolean again.

I've completed the rewrite. My application still works and the unit test that was already there for FileAppender succeeds:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.apache.logging.log4j.core.appender.FileAppenderTest
Tests run: 6, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 4.713 sec - in org.apache.logging.log4j.core.appender.FileAppenderTest

Results :

Tests run: 6, Failures: 0, Errors: 0, Skipped: 1

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:18 min
[INFO] Finished at: 2015-08-12T17:59:18+02:00
[INFO] Final Memory: 22M/336M
[INFO] ------------------------------------------------------------------------

But then, I haven't used the builder yet.

Now that I have, it works. However the status logger messages given from these classes don't work:

System.setProperty("org.apache.logging.log4j.simplelog.StatusLogger.level", "WARN");

// adds a logger / appender that should generate an error message

Output:

WARN StatusLogger Multiple logging implementations found:
Factory: org.apache.logging.log4j.core.impl.Log4jContextFactory, Weighting: 10
Using factory: org.apache.logging.log4j.core.impl.Log4jContextFactory
Exception in thread "main" java.lang.NullPointerException
        at thunderbolt.log.Logging.init(Logging.java:87)
        at thunderbolt.Server.main(Server.java:230)

The NullPointerException is completely expected as the method returned NULL due to a required parameter missing. I just failed to supply a filename. A quick interjected System.out message proves it:

WARN StatusLogger Multiple logging implementations found:
Factory: org.apache.logging.log4j.core.impl.Log4jContextFactory, Weighting: 10
Using factory: org.apache.logging.log4j.core.impl.Log4jContextFactory
*Trying to write output error message: .....**
*Exception in thread "main" java.lang.NullPointerException
        at thunderbolt.log.Logging.init(Logging.java:87)
        at thunderbolt.Server.main(Server.java:230)

*Should I post a JIRA of this as a separate issue?.*

I have a working modified FileAppender that can use the Builder Paradigm to construct the object and it is working. I could write/extend the unit test for FileAppender (FileAppenderTest) to account for it and post a dual update as a Jira ticket/patch. The code might not be pretty according to your standards but I've done my best to single out the sanity checks in accordance with a mutable boolean object/class. I have replicated and mirrored the ConsoleAppender in this as much as possible, the code for the builder follows the same style and annotations.

Essentially the create method could use the builder without any problem. It's just that it would/might need to use all of the methods, devoiding the benefit/idea of using a builder. You could rewrite it to only use the methods for parameters that are not null/invalid. Then the builder could handly sanity and error messages. It would not be all that ugly and probably not uglier than what I have now, but at this point I have a centralized sanity checking function.

Let me know if you want to see the source or you want a diff our you want a Jira ticket with a diff.

Regards, Bart.

-----------------------------------------------

Anyway I'm holding silence for a while, going to do some other things I guess.

Maybe commit suicide ;-). By not eating lol. May take me a while. Maybe I should start smoking, that should help.

Regards..

Reply via email to