Using method handles to get logger

One more quote from The Well-Grounded Java Developer by Benjamin J. Evans and Martijn Verburg about useful feature of MethodHandle.


One additional feature that method handles provide is the ability to determine the current class from a static context. If you’ve ever written logging code (such as for log4j) that looked like this,

Logger lgr = LoggerFactory.getLogger(MyClass.class);

you know that this code is fragile. If it’s refactored to move into a superclass or subclass, the explicit class name would cause problems With Java 7, however, you can write this:

Logger lgr = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

It’s really works :)

Tags:
comments powered by Disqus