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.

  • 3
    Java already has 17 logging frameworks and meta-logging frameworks (slf4j) and probably some meta-meta-logging framework and none of them work for you? Nov 11, 2014 at 3:45

4 Answers 4


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

  • 1
    Is this basically saying "write to your abstract logger in your code"? I guess my question is: should I trigger events from where I want to log and provide a listener which logs in response to events, or should I straight call my Logger service (which uses chain of responsability internally) and that's it? Apr 15, 2017 at 8:46

Use Aspect Oriented Programming which uses After, Before and Around advices over methods. There, as per your need, you can add logs before start of api, after or on some conditions and also separate your main code from logging code.


Well, Observer sounds unfit to me. Also, tossing logger calls "wherever you need" will shatter your code and violate the SRP.

You might - for example - be interested in AOP for this, so you can attach logger calls via method annotations.


Chain of responsibility seems to be a good pattern when your output can landup at multiple places. In the UML you have different logger one that directs to console, other to errorFile and 3rd simply info logger.

Usually I have seen that logLevels are different but the logging file is same.

I don't see observer pattern as bad for logging as it decouples your logging code from application code. Which is a good practise, migrating to different logging mechanisms is easy this way. Every time you want to log, fire an event and the appropriate listener will recieve the event and log it. There should be an intermediate singleton object that holds the list of all registers.

This way I see that we can decouple our logging code from application code.

Not the answer you're looking for? Browse other questions tagged or ask your own question.