How to get the current class name in Java with log4j

java, log4j

Solution

Solution for static logger:

private static final Logger LOG = Logger.getLogger(new Object() { }.getClass().getEnclosingClass());

Problem

am using log4j for logging, in order to get the class name of the of the respective methods while executing, i got some common method which uses SecurityManager to get the class name, but i dont want to use SecurityManager, is their any other way to get the class name during runtime. Also i dont want to write the code(`MyClass.getClassName`) to get the classname in each and every class. ``` class log extends SecurityManager { public String getClassName() { return getClassContext()[3].getName(); } } ```

Original source