Java Logging With Abstract Classes
This is my solution (final static logger):
public abstract class AbstractFoo {
protected abstract Log getLogger();
public doSomething() {
getLogger().info("log something");
}
}
public class Foo extends AbstractFoo {
private static final Log log = Log.getLogger(Foo.class);
protected Log getLogger() {
return log;
}
public doSomethingElse() {
log.info("log somethingElse");
}
}
I wouldn't do either. Instead I would make it use the correct class in both cases.
public abstract class AbstractFoo {
protected final Log log = LogFactory.getLog(getClass());
...
}
public class Foo extends AbstractFoo {
public void someMethod() {
log.info("Using abstract log");
}
}
If you are not doing lots of logging (which is a good idea anyway) you can use a method instead.
public abstract class AbstractFoo {
protected Log log() { return LogFactory.getLog(getClass()); }
...
}
If there is a class which calls this a lot you can override it to give you a cached instance.