• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Using Reflection to dynamically load logging classes

Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the blog:

What we eventually realised was that no one really cares about our
logging but us so what we did was not use logging at all by default
it is loaded by reflection so no external logging classes are
referenced unless the user explicitly enables them

Ok... what is my first clue on how to do this? (aside from asking in poi-developer, or just looking the source, since it's Open Source.

My library classes all use log4j, and so my libraries instantly require log4j. I'd love to be able to have them run with or without.

Has anyone attempted this sort of thing before?
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know his exact situation, but I've done a number of things to reduce package dependencies in my own little projects. For example, what if all logging statements used an object that was defined only by an interface.

Now the code has no idea what the actual logger class is.
But that test for null is ugly and you have to do it every time you log. Easy to forget. How about a special NullLogger that does nothing?

This kind of thing is common in discussions of package dependencies. Here's a presentation I did for my team: http://www.surfscranton.com/architecture/DependencyInversion.htm. It has a picture of Robert Martin's book which is a treasure trove of good stuff like this.
Hope that answer matched your question. Look into the OO, UML etc. forum further down the ranch for lots more talk about such design principles.
I am mighty! And this is a mighty small ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!