Java – Best Design Pattern for Logging

design-patternsjavaloggingobserver-pattern

I should log some events in a program but as far as I know it would be better to keep the logging code outside the program because it is not about the real functionality of the program. So may you tell me if I should keep it completely out of code and only use Observers and Listeners to log the events? Or I can add a line of code like the following wherever I need to log something:

MyGloriousLogger.getXXXLogger().Log(LogPlace, new LogObject(z1, z2, z3, z4, ..., z99));

Do I make a mistake to use Observer design pattern? I need another design pattern? Or I should stop thinking about design patterns?

PS1. If I want to log using only listeners and observers I will certainly need to add and improve the observers and listeners of the program.

PS2. I certainly know that there are different libraries for logging in Java and I'm using java.utils.logging but I need to have a wrapper for it to log my special objects.

Best Answer

Logging is usually implemented with the Chain of responsibility pattern. Of course you can (and I would) combine that with a Facade. I really wouldn't use Listener(s) or Observer(s) myself.

Chain of responsibility - Logger