2

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

Jas
  • 14,493
  • 27
  • 97
  • 148

4 Answers4

7

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.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • 1
    +1 I was thinking with slf4j in mind and couldn't understand the question ! – Stefanos Kalantzis Jan 04 '13 at 12:15
  • Please do not inflict slf4j on the poor chap. – Perception Jan 04 '13 at 12:47
  • @Perception Can you elaborate? – assylias Jan 04 '13 at 12:48
  • @assylias - in any project of anything but modest complexity you will likely run into the slf4j multiple binding issue. – Perception Jan 04 '13 at 12:53
  • @Perception I have never run into that problem, but point taken. – assylias Jan 04 '13 at 13:01
  • @assylias so if i get it correctly i can replace the implementation for the logger i would get, so in my junit ie i would do it in setup and use something like a noop logger, however if at some other testcase i also replace the logger that returns by loggerfactory i have just ruined the further tests, because its like a global variable, so DI was supposed to resolve a global var problem but correct me if i'm wrong by using here slf4j as suggested i'm having the loggerFactory and the loggers out of it again as globals. – Jas Jan 04 '13 at 13:11
  • @Jason I don't think you can change the logging library programmatically - if that's what you need then you will need a DI but that sounds really uncommon. If what you need is to change the behaviour of your logger, then most libraries provide ways to be configured programmatically. For example, with logback, you can programmatically change the behaviour of the logger ([link](http://logback.qos.ch/manual/configuration.html#joranDirectly)) at any time, including making all log calls a noop. – assylias Jan 04 '13 at 13:18
  • @Jason Alternatively, you can also mock the logging library in your tests and turn all logging calls to a noop. – assylias Jan 04 '13 at 13:18
4

Loggers is one example where most people don't use DI even if they have DI framework.

There is nothing stopping you using them via DI, e.g. for unit tests.

class MyClass {
    final Logger logger;

    public MyClass() {
        this(Logger.getLogger(getClass().getName());
    }

    public MyClass(Logger logger) {
        this.logger = logger;
    }
}

BTW: I use DI without a framework, and I try to keep the number of components which can log to a minimum. e.g. I have listener interfaces which can take errors and warnings. For those that do logging, I let them get their own loggers.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

Any good idea taken to extremes will become absurd... so don't try to inject everything - there are often a few things that class can and should control itself, logger is one of them. BTW: unless you want to change logger after initialization you better add final to private static, that's kinda good practice.

Andrey Nudko
  • 697
  • 5
  • 5
1

It makes no sense to inject into a static property. First you should decide if you want to use per class (static) or per instance loggers. Have a look at When Static References to Log objects can be used, or at this question. Those posts also show some ways to configure the loggers.

Community
  • 1
  • 1
pgras
  • 12,614
  • 4
  • 38
  • 46