Package Com.doctor.slf4j;import Org.junit.test;import Org.slf4j.logger;import org.slf4j.loggerfactory;/** * How to print exception stack information. * @author Doctor * * @time December 11, 2014 morning 9:49:00 */public class Logthrowablerule {private final Logger log = Loggerfacto Ry.getlogger (GetClass ());/** * This e.tostring () method just gets the exception information, the exception stack does not get */@Testpublic void test_wronglogthrowable () {try { int test = 10/0;} catch (Exception e) {log.error ("error" + e);//main error C.d.s.logthrowablerule-//Errorjava.lan G.arithmeticexception:/By zero}}/** * Get exception stack information for correct log method */@Testpublic void test_rightwaygetinfoforthrowable () {int a = 10 ; int B = 0;try {int test = A/b;} catch (Exception e) {log.error ("error", e);//main error C.d.s.logthrowablerule-//error/ /java.lang.arithmeticexception:/by zero//at com.doctor.slf4j.LogThrowableRule.test_rightWayGetInfoForThrowable (logthrowablerule.java:35) ~[classes/:na]//at Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) ~[na:1 .8.0_11]// ............. String msg = String.Format ("{error: '%s/%s '}", A, b);//string with parameters, using String.formatlog.error (MSG, e);}}
How the log prints exception stack information.