Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #20947

Re: dependency injection and loggers

From Daniel Pitts <newsgroup.nospam@virtualinfinity.net>
Newsgroups comp.lang.java.programmer
Subject Re: dependency injection and loggers
References <e3230fa7-b672-49e0-a7ef-dfb55a9180b5@googlegroups.com> <kc6rjn$fl2$1@dont-email.me>
Message-ID <veFFs.21649$6a5.5075@newsfe14.iad> (permalink)
Date 2013-01-04 10:31 -0800

Show all headers | View raw


On 1/4/13 7:13 AM, markspace wrote:
> On 1/4/2013 4:00 AM, Tomer wrote:
>> I love the concept of dependency injection via ctor. it simplifies
>> life and makes testing easy. 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?
>
>
> I agree with you on both counts.  Ctors are an excellent way of
> implementing dependency injection, and static methods aren't.
>
> However loggers are more of an aspect than a dependency.  Absent some
> other framework (AOP, for example, or some sort of annotation
> processing), good old frameworks and libraries solve this problem.  Have
> a look at Apache logging:
>
> <http://commons.apache.org/logging/>
>
>
Loggers can generally be configured externally to their instances. 
Therefor you don't usually need to inject them.

On the other hand, there generally isn't any real requirement that 
loggers be static.  I have had occasion to create a class which uses a 
different logger depending on the context in which that class was created.

public class MySomething {
    private final Logger log;

    public MySomething() {
       this.log = Logger.getLogger(MySomething.class);
    }
    public MySomething(Logger log) {
       this.log = log;
    }
}

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

dependency injection and loggers Tomer <tomerbd1@gmail.com> - 2013-01-04 04:00 -0800
  Re: dependency injection and loggers markspace <markspace@nospam.nospam> - 2013-01-04 07:13 -0800
    Re: dependency injection and loggers Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2013-01-04 09:18 -0800
      Re: dependency injection and loggers Arne Vajhøj <arne@vajhoej.dk> - 2013-01-04 22:27 -0500
        Re: dependency injection and loggers Wayne <nospam@all.invalid> - 2013-01-04 23:33 -0500
          Re: dependency injection and loggers Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-01-05 11:55 -0400
            Re: dependency injection and loggers Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2013-01-05 10:01 -0800
              Re: dependency injection and loggers Arne Vajhøj <arne@vajhoej.dk> - 2013-01-05 15:17 -0500
              Re: dependency injection and loggers Arne Vajhøj <arne@vajhoej.dk> - 2013-01-05 16:37 -0500
        Re: dependency injection and loggers Lew <lewbloch@gmail.com> - 2013-01-05 12:24 -0800
          Re: dependency injection and loggers Arne Vajhøj <arne@vajhoej.dk> - 2013-01-05 16:41 -0500
    Re: dependency injection and loggers Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-01-04 10:31 -0800
      Re: dependency injection and loggers Lew <lewbloch@gmail.com> - 2013-01-04 11:36 -0800
        Re: dependency injection and loggers Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-01-04 11:57 -0800
      Re: dependency injection and loggers Arne Vajhøj <arne@vajhoej.dk> - 2013-01-04 22:28 -0500
  Re: dependency injection and loggers Arne Vajhøj <arne@vajhoej.dk> - 2013-01-04 22:24 -0500

csiph-web