When using log4j Time , If we call
logger.info("123");
There is only one line , Even if the parameter is a Exception object , It's just that the information of the output object itself is still only one line .

Want to be like e.printStackTrace() Output stack information as well , It can be used logger The following methods :
logger.error("123",e);
This is a string Exception Methods .

Only string parameters are not allowed .

Only the Exception Parameters don't work .

 

Examples :
public static void main(String[] args){ methodA(); } public static void
methodA(){ methodB(); } public static void methodB(){ RuntimeException e = new
RuntimeException(); logger.error("123",e); }
Output results :

15:00:44.171 [main] ERROR Test - 123

java.lang.RuntimeException: null

       at Test.methodB(Test.java:469)

       at Test.methodA(Test.java:465)

       at Test.main(Test.java:461)

 

 

In addition to the above method , There's another stupid way , From Exception Object to get stack information directly and output , Like this :
public static void main(String[] args) { methodA(); } public static void
methodA() { methodB(); } public static void methodB() { methodC(); } public
static void methodC() { Exception e = new Exception(); StackTraceElement[] arr
= e.getStackTrace(); for (int i = 0; i < arr.length; i++) {
System.out.println(" " + arr[i].toString()); } System.out.println(" Output complete "); }
adopt e.
getStackTrace() method , You get one StackTraceElement array ,StackTraceElement Object call toString() Method , What you get is a stack message .

The output of the above code is :

java.lang.Exception

  Test.methodC(Test.java:16)

  Test.methodB(Test.java:12)

  Test.methodA(Test.java:8)

  Test.main(Test.java:3)

Output complete

Technology