Rational
I would like to have more detailed logging debugging/logging output on a few superclasses, so the subclass name will not actually be effecting real-time performance nor application logic. I’m also of the opinion that it is fairly bad design to do this normally, however I really do want this info to be logged during development runs.
Question
Suppose I have a simple class structure as below, how do I pull the name of the subclass into the superclass without having to explicitly pass a reference?
public abstract AbstractClass { private Logger logger = LoggerFactory.getLogger(getClass()); public AbstractClass() {} public void execute(ContextObject context) { if (logger.debuggingEnabled()) { String invokingClassName = ''; // <-- how do I get this? logger.debug('Executing {}', invokingClassName); } // shared application logic is here... } } public MyClass extends AbstractClass { public MyClass() {} @Override public void execute(ContextObject context) { super.execute(context); // application logic... } }
I have worked with parsing stack traces before and I suspect that is the route I will have to go if I want this information. Does anyone know of a library that will do this for me or a good code snippet?
Doesn’t
this.getClass().getName()do the trick? Namely: