How to send a stacktrace to log4j?

后端 未结 11 1001
失恋的感觉
失恋的感觉 2020-11-29 19:29

Say you catch an exception and get the following on the standard output (like, say, the console) if you do a e.printStackTrace() :

java.io.FileNotFo         


        
11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 20:05

    If you want to log a stacktrace without involving an exception just do this:

    String message = "";
    
    for(StackTraceElement stackTraceElement : Thread.currentThread().getStackTrace()) {                         
        message = message + System.lineSeparator() + stackTraceElement.toString();
    }   
    log.warn("Something weird happened. I will print the the complete stacktrace even if we have no exception just to help you find the cause" + message);
    

提交回复
热议问题