2021年4月11日星期日

how to print the root invoke class name of the invoke?

In log4j2, there are 2 classes:

Common.java

public class Common {      protected static Logger logger = LogManager.getLogger("mts_logger");        public static void sayHi(String hi){          logger.info(hi);      }  }  

Demo1.java:

public class Demo1 {      @Test      public void test1(){          Common.sayHi("hello");      }  }  

and the printed log is :

2021-04-09 12:10:27.652  INFO   -utils.Common.sayHi(Common.java:14) mts_logger  Common.java - world    

the log4j2.xml pattern is:

<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level  -%l %c - %msg%n" />  

We could see that the log only print Common.java, did not print the class name of Demo1 , I just want that the log could show the original class name which invoked the method "sayHi", in this example, the class name is Demo1. How to config log4j2 then it could show the "original" invoke class name "Demo1" too ?

In one word, how to make it to print Demo1 in the log ?

https://stackoverflow.com/questions/67015027/how-to-print-the-root-invoke-class-name-of-the-invoke April 09, 2021 at 12:21PM

没有评论:

发表评论