Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current class name in Java with log4j

Tags:

java

log4j

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();
    }

}
like image 507
user1787641 Avatar asked Dec 06 '22 08:12

user1787641


1 Answers

Solution for static logger:

private static final Logger LOG = Logger.getLogger(new Object() { }.getClass().getEnclosingClass());
like image 186
Michal Krasny Avatar answered Dec 22 '22 00:12

Michal Krasny