Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: markspace Newsgroups: comp.lang.java.programmer Subject: Re: dependency injection and loggers Date: Fri, 04 Jan 2013 07:13:57 -0800 Organization: A noiseless patient Spider Lines: 20 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 4 Jan 2013 15:13:59 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="61282af8d6595e8d991edb5ac03d6e00"; logging-data="16034"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+lvl3UQlP821UBc+PKWIkB6ZwHdMj09rk=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: Cancel-Lock: sha1:sfJZsdpg7LuTg6LHJBehDdVcwh8= Xref: csiph.com comp.lang.java.programmer:20942 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: