how to apply logger via dependency injection in constructor and no frameworks

java, oop

Solution

One solution is to use a facade framework, such as slf4j. What `Logger.getLogger(...)` then does is actually fetch the logging library and use it. Changing logging libraries is then a simple matter of configuration.

The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks, e.g. java.util.logging, log4j and logback, allowing the end user to plug in the desired logging framework at deployment time.

So it is very similar to using DI, but probably more appropriate in this case.

Problem

I'm doing dependency injection with no framework (no spring/guice) just plain java (I need to). My question is about loggers what about logger? i usually instantiate it in ``` private static logger = Logger.getLogger(myclass); ``` however this is not dependency injection, should I pass the logger into each ctor? this would look wierd... so what to do about loggers and depedency injection? PS I prefer DI with ctor and not setters in this way i know exactly what my classes need. thanks

Original source

Related problems