Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > dependency injection and loggers

Reply
Thread Tools

dependency injection and loggers

 
 
Tomer
Guest
Posts: n/a
 
      01-04-2013
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?

thanks
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      01-04-2013
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/>


 
Reply With Quote
 
 
 
 
Kevin McMurtrie
Guest
Posts: n/a
 
      01-04-2013
In article <kc6rjn$fl2$>,
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/>


I'd say that Java's own logger (java.util.logging) is worth a look too
if you want modularity. It's easy to plug in custom formatters and
handlers. You map logging paths to handlers during app initialization
so no dependency injection is involved. My biggest peeve about most
loggers is that they don't indent multi-line log entries nicely and
they're sensitive to disk latency. It's not a problem when you plug in
your own parts. You can also do fancy logging to remote archiving
systems (JSON or XML over socket or REST, etc.) with little effort.

http://docs.oracle.com/javase/6/docs...package-summar
y.html
--
I will not see posts from Google because I must filter them as spam
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      01-04-2013
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;
}
}


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-04-2013
Daniel Pitts wrote:
> 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;
> }
> }


If I have a class hierarchy (for when despite Josh Bloch's recommendation to "prefer composition
to inheritance" I go the other way) I will often put in the parent class:

private final Logger logger = Logger.getLogger(getClass().getName());

This despite the violation of "Don't call an overrideable method during construction."



--
Lew
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      01-04-2013
On 1/4/13 11:36 AM, Lew wrote:
> Daniel Pitts wrote:
>> 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;
>> }
>> }

>
> If I have a class hierarchy (for when despite Josh Bloch's recommendation to "prefer composition
> to inheritance" I go the other way) I will often put in the parent class:
>
> private final Logger logger = Logger.getLogger(getClass().getName());
>
> This despite the violation of "Don't call an overrideable method during construction."
>
>
>

getClass() is not overrideable.
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      01-05-2013
On 1/4/2013 7: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?


If you do not use a logger hierarchy, then DI'ing loggers would be easy.

But you typical do so DI is difficult.

Arne




 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      01-05-2013
On 1/4/2013 12:18 PM, Kevin McMurtrie wrote:
> In article <kc6rjn$fl2$>,
> markspace <> wrote:
>> 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/>

>
> I'd say that Java's own logger (java.util.logging) is worth a look too
> if you want modularity. It's easy to plug in custom formatters and
> handlers.


log4j has more than jul.

But if jul has sufficient, then it is obviously fine.

> My biggest peeve about most
> loggers is that they don't indent multi-line log entries nicely


I would recommend avoiding multi-line entries completely.

Arne

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      01-05-2013
On 1/4/2013 1:31 PM, Daniel Pitts wrote:
> On 1/4/13 7:13 AM, markspace wrote:
>> 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.


Typically non static logger refs will point to same
logger object.

Arne

 
Reply With Quote
 
Arved Sandstrom
Guest
Posts: n/a
 
      01-05-2013
On 01/05/2013 12:33 AM, Wayne wrote:
> On 1/4/2013 10:27 PM, Arne Vajhøj wrote:
>> On 1/4/2013 12:18 PM, Kevin McMurtrie wrote:
>>> In article <kc6rjn$fl2$>,
>>> markspace <> wrote:
>>>> 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/>
>>>
>>> I'd say that Java's own logger (java.util.logging) is worth a look too
>>> if you want modularity. It's easy to plug in custom formatters and
>>> handlers.

>>
>> log4j has more than jul.
>>
>> But if jul has sufficient, then it is obviously fine.
>>
>>> My biggest peeve about most
>>> loggers is that they don't indent multi-line log entries nicely

>>
>> I would recommend avoiding multi-line entries completely.
>>
>> Arne
>>

>
> Arne is right. Permitting multi-line log entries is a security
> hazard. (It is safe to format log entries with multiple lines; but
> you need to sanitize user data (e.g., strip CR/LF) before including
> such data in your log messages.)
>

Errr, having multiline log entries is not a security hazard, not even
remotely. Splunk certainly doesn't think so - that toolset has extensive
configuration capabilities for handling multiline entries.

What *is* a hazard is if external input to a log entry, that might be
maliciously seeded with linefeeds, is not sanitized/conditioned. Which
is what you said. This is actually a CERT secure coding guideline. Point
being, if you are controlling log entry input, and *you* want a
linefeed, fill your boots. It's not insecure. Just don't let accidental
or malicious ones be supplied across a trust boundary.

I do agree with Arne that multiline should be avoided. Mainly because
it's a PITA. Not everyone is using Splunk.

AHS
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic Question about Loggers, Parent Loggers, and Levels Rhino Java 8 04-30-2010 10:20 PM
What is Dependency Injection? Anonieko ASP .Net 2 10-14-2007 12:03 PM
Interface coding by contract design and dependency injection PerfectDayToChaseTornados Ruby 5 01-30-2007 08:38 PM
Dependency Injection in C++ Michael Feathers C++ 5 10-13-2006 08:23 AM
How to build a dependency injection framework by myself? =?iso-8859-1?B?bW9vcJk=?= Java 4 08-24-2006 06:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57