Link Details

DZone can't read your mind. Tell us what you like! Login and vote now.
Link 83164 thumbnail
User 246188 avatar

By kbilsted
via firstclassthoughts.co.uk
Published: May 27 2008 / 17:39

The standard way to acquire Log4j loggers do not ensure consistency between logger name and class using the logger. Such inconsistency leads to inconsistencies in the log files, rendering any information in the log file difficult to use. This article shows how to ensure between logger name and class using the logger through usage of the Spiffy Framework.
  • 10
  • 3
  • 1613
  • 562

Comments

Add your comment
User 191349 avatar

raveman replied ago:

0 votes Vote down Vote up Reply

sorry, pointless. you can check the static version at compile time using checkstyle, etc.

User 246188 avatar

kbilsted replied ago:

0 votes Vote down Vote up Reply

I'm not sure I understand your negative vote. Log4J obviously have a serious flaw in how loggers are defined. You are probably right that you can use some third-party tool to check what you've done is correct, but that does not negate the fact that the approach I suggest for acquiring loggers are easier and will ALWAYS work. Conventions are easiest upheld when they are automatic...

User 187417 avatar

sproketboy replied ago:

1 votes Vote down Vote up Reply

Interesting idea.

User 246188 avatar

kbilsted replied ago:

0 votes Vote down Vote up Reply

Thanks.
Yes the idea of implementing a 'thisclass' as done here can be used in a number of places. I found it really useful for the Log4J stuff, hence my blog...

User 53369 avatar

behrangsa replied ago:

0 votes Vote down Vote up Reply

You can also inject the logger at runtime:


@Logger private log;


You will need some extra libs in your project though.

User 53369 avatar

behrangsa replied ago:

1 votes Vote down Vote up Reply

Oops, I meant:


@Logger private Logger log;

User 246188 avatar

kbilsted replied ago:

0 votes Vote down Vote up Reply

Hi behrangsa

thanks for the comment. What framework are you using for this?
Can you make the log variable static? if not you are not much better off than using the Logger.getLogger( this.getClass() )

User 255959 avatar

yardena replied ago:

1 votes Vote down Vote up Reply

IntelliJ code generation template worked for me, so I haven't really looked for other ways. Guice (http://code.google.com/p/google-guice) is supposed to be able to do this kind of things. Another trick I can think of:

class ... {

final static Class me = new Whatever() {}.getClass().getEnclosingClass();
}

User 246188 avatar

kbilsted replied ago:

0 votes Vote down Vote up Reply

That's a nice one, I should probably include this in the article. Its JDK5+ only isn't it?

User 255959 avatar

yardena replied ago:

0 votes Vote down Vote up Reply

Yup. There is also a getDeclaringClass() http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getDeclaringClass() but one time I tried it, it didn't return what I expected.

User 246188 avatar

kbilsted replied ago:

0 votes Vote down Vote up Reply

Thanks for your comments. I think its worth extending the article with annonymous subclasses and loggers.. it surely is something that you can't check with checkstyle! :-)

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.