tags:

views:

110

answers:

4

Right now I am doing something like this:

 private static Logger logger = LoggerFactory
            .getLogger(MasterController.class);

Is there a better way than using the name of the class (which is MasterController)? Maybe something generic?

A: 

Not sure what you need but you can always call getClass() on an Object

Fredrik
since the logger is static, I can't use "this" in a static context..I tried doing "this.getClass()" instead of "MasterController.class"
tim
@Tim: Ok, it wasn't clear that you needed to do it in a static context just because the logger itself is declared static.
Fredrik
hey, that anonymous downvote wasn't really fair, was it? You could at least spend ten seconds motivating it.
Fredrik
+1  A: 

How about this:

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

This approach avoids copy+paste errors.

I've read (don't remember where, was years ago) that Logger instances are terribly cheap and it doesn't matter if each instance of your class has its own logger.

But if you're not persuaded, and you want to keep the Logger instance static, then it should probably be both final and in uppercase, like so:

private static final Logger LOG = LoggerFactory.getLogger(MasterController.class);
Drew Wills
thanks for the answer. could you please explain why it should be final as well?
tim
Are you going to point the logger variable at a different Logger object at some point? If not, I think 'final' is more idiomatic. It also may allow some compilers to make optimizations on some platforms.
Drew Wills
"Logger instances are terribly cheap" - I'm betting there's still only one real `Logger` *instance* per class (created by `LoggerFactory` the first time it's called with each class, and reused for later calls using the same class). You're really just adding a `Logger` *reference* in each instance of the class that's using it rather than a single static reference. So you're increasing the size of each instance (very slightly), but not using any additional memory creating `Logger` instances.
Nate
@Nate: You are exactly correct. (I had to dig into log4j to understand somethings about it's LoggerHierarchy to manage separate ones for different virtual servers.)
Kevin Brock
@Drew: Yes, without `final` some code some where might reset the logger or even set it to `null` and then all the logger calls would fail. This is a very useful aspect of final - protecting from simple programming errors.
Kevin Brock
+1  A: 

The following link has an interesting approach:

http://www.rgagnon.com/javadetails/java-0402.html

Pasted in here for convenience:

public class ClassFromStatic {

  public static void main(java.lang.String[] args) {
    someStaticMethod();
  }

  public static void someStaticMethod() {
    System.out.println
       ("I'm in " + new CurrentClassGetter().getClassName() + " class");
  }

  public static class CurrentClassGetter extends SecurityManager {
    public String getClassName() {
      return getClassContext()[1].getName();
    }
  }
}
dpb
Just wanted to say that personally, I think for utility classes, I would prefer seeing it as the way you have originally wrote it.
dpb
+2  A: 

Have a look at: http://www.javaspecialists.eu/archive/Issue137.html

rhu