Java logger that automatically determines caller's class name

后端 未结 21 804
礼貌的吻别
礼貌的吻别 2020-12-07 17:53
public static Logger getLogger() {
    final Throwable t = new Throwable();
    final StackTraceElement methodCaller = t.getStackTrace()[1];
    final Logger logger          


        
21条回答
  •  太阳男子
    2020-12-07 18:33

    You could of course just use Log4J with the appropriate pattern layout:

    For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass".

    http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

提交回复
热议问题