I have a subclass and a superclass. In the superclass I have a method that logs something. When I create an instance of the subclass the logger creates a logging message for the super class. Why is this Happening?
Code example:
SuperClass.java
import java.util.logging.Level;
import java.util.logging.Logger;
public abstract class SuperClass {
public void logAndPrintClass(){
String name = this.getClass().getName();
System.out.println(name);
Logger logger = Logger.getLogger(name);
logger.log(Level.INFO, "Logmessage");
}
}
SubClass.java
public class SubClass extends SuperClass {
}
TestLogBubClass.java
public class TestLogBubClass {
public static void main(String[] args){
SuperClass obj = new SubClass();
obj.logAndPrintClass();
}
}
Output:
SubClass
Mar 15, 2013 6:30:04 PM SuperClass logAndPrintClass
INFO: Logmessage
As you can see the name of the class is correctly printed, but incorrectly represented in the log message.
LogRecord
object, which is hidden behind the call tologger.log()
is being created in the subclass. – Brod