[slf4j-dev] [Bug 163] Copy & paste of LoggerFactory.getLogger

bugzilla-daemon at pixie.qos.ch bugzilla-daemon at pixie.qos.ch
Thu Dec 10 19:26:25 CET 2009


http://bugzilla.slf4j.org/show_bug.cgi?id=163





--- Comment #3 from Thomas Mueller <thomas.tom.mueller at gmail.com>  2009-12-10 19:26:24 ---
I didn't know about that... I agree this shouldn't be used (except maybe for
testing). I wonder when in practice this would fail, do you know a case where
the stack trace is wrong? Just for completeness, I guess this would always
work, but only on a Sun JVM: sun.reflect.Reflection.getCallerClass(). I don't
suggest to use it however.

Another idea. What about:

1) LoggerFactory.getEnclosingLogger(new Object(){});

which would be a shortcut for:

2) LoggerFactory.getLogger(new Object(){}.getClass().getEnclosingClass());

This is copy & paste safe. It will generate an anonymous inner class, create
one instance, and get the enclosing class. Instead of implementing 1) you could
just document 2) somewhere. Disadvantages:

- One additional class per logger (about 500 bytes; not sure how much if
compressed).

- Hard to understand and therefore possible to use incorrectly (users might
forget the {} and then complain it doesn't work) - so better throw a nice error
message if you implement getEnclosingLogger(Object obj).


-- 
Configure bugmail: http://bugzilla.slf4j.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.


More information about the slf4j-dev mailing list