I think the common idiom for creating instances of java.util.logging.Logger is this:
public class SomeClassName {
private static final Logger LOG = Logger.getLogger(SomeClassName.class.getName());
}
My IDE will manage changing the line appropriately when I refactor my code (change the name of the class, for example). It still bugs me that I have to repeat the name of the class, though. What I’d really like to do is something like Logger.getLogger(getName()) or Logger.getLogger(class.getName()), but this isn’t legal Java in a static initilization.
Is there a better way of getting at a logger that doesn’t involve repeating myself?
Issue 137 of The Java Specialists’ Newsletter deals with this problem. It recommends applying a logger factory, which can detect the actual class name e.g. by generating an exception and analysing the call stack.
I personally find this worse than the original problem, but this is just my 2 cents. At any rate, technically it is interesting, so here it is: