11

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?

James Marble
  • 863
  • 1
  • 10
  • 22
  • `Logger logger = new logger.getLogger` - I'd be more worried about the impending logpocalypse! – Jeff Foster Mar 30 '12 at 15:36
  • FYI, see an explanation in the [sl4j FAQ](http://www.slf4j.org/faq.html#declaration_pattern), different framework but probably same rationale. – assylias Mar 30 '12 at 15:39
  • Thanks, @assylias! That FAQ addresses my specific concern very clearly. Namely that "the above logger declaration idiom is not resistant to cut-and-pasting between classes." It does not provide a solution, however. – James Marble Mar 30 '12 at 16:19

5 Answers5

10

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:

public class LoggerFactory {
  public static Logger make() {
    Throwable t = new Throwable();
    StackTraceElement directCaller = t.getStackTrace()[1];
    return Logger.getLogger(directCaller.getClassName());
  }
}

...

public class BetterApplication {
  private final static Logger logger = LoggerFactory.make();

  ...
}
Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • 1
    This addresses my specific concern, but I agree that it is worse than the original problem and I won't be using it in my code. Thanks for the ... interesting solution, though! :) – James Marble Mar 30 '12 at 16:21
8

I create an Eclipse code template and use it each time.

enter image description here

You just have to type logger and press Ctrl + Space to activate it.

adarshr
  • 61,315
  • 23
  • 138
  • 167
  • 3
    I don't think that's what was asked, as OP specifically mentions that his IDE manages this for him. – Niklas B. Mar 30 '12 at 15:33
  • 2
    I think the code template tip is valid as the OP mentions his IDE manages changes to the class name, not the original generation of the line. – davidfrancis Mar 30 '12 at 15:35
  • 1
    PS You can now include imports in the Eclipse code template - makes it even easier. – davidfrancis Mar 30 '12 at 15:35
  • 1
    See here: http://stackoverflow.com/questions/1028858/useful-eclipse-java-code-templates – davidfrancis Mar 30 '12 at 15:39
  • This doesn't address my specific concern that repeating yourself in this case leaves you vulnerable to copy-paste errors. This is similar to what I currently do. I have a static logger instantiation in my class template. I think your way is nicer, though. I don't have to add an instance until I know I need it. – James Marble Mar 30 '12 at 16:26
1

We do this:

private Logger log = Logger.getLogger(this.getClass());
k-den
  • 853
  • 13
  • 28
1

I use a plugin called log4e which is handy for logging.

http://log4e.jayefem.de/

You can use it to automatically add before/after logging for a method or a whole class.
Also you can get it to auto replace System.out.println's with logger statements.
Very handy.

davidfrancis
  • 3,734
  • 2
  • 24
  • 21
0

There is a trick to get the name of current class from a static context, I can't cite it from memory but it involved throwing an exception inside the static block and reading stack trace to get to the name of the class. However, it's quite a hack, so in practice I find repeating myself better than playing such tricks.

Michał Kosmulski
  • 9,855
  • 1
  • 32
  • 51