Spring AOP can not destroy our program code under the premise of a good program to print the exception, there are many such examples on the Web, I write here is relatively simple, just for the program to see the exception of the log printing, the code is relatively simple.
Exception Log Processing class
Package Com.apt.study.exception;import Org.aspectj.lang.joinpoint;import Org.aspectj.lang.annotation.afterthrowing;import Org.aspectj.lang.annotation.aspect;import Org.aspectj.lang.annotation.pointcut;import Org.slf4j.logger;import Org.slf4j.loggerfactory;import Org.springframework.stereotype.component;import com.alibaba.fastjson.JSONObject; @Component @aspect Public classExceptiontojson { PublicLogger Logger = Loggerfactory.getlogger (Exceptiontojson.class); @Pointcut ("Execution (* com.apt.study.service). *.*(..))") Public voidExceptionlog () {} @AfterThrowing (Pointcut="Exceptionlog ()", throwing="e") Public voiddoafterthrowing (Joinpoint joinpoint, Throwable e) {Try{logger.error ("------->error Class:"+E.getclass (). GetName ()); Logger.error ("------->error msg:"+e.getmessage ()); Logger.error ("------->error Method:"+ (Joinpoint.gettarget (). GetClass (). GetName () +"."+ joinpoint.getsignature (). GetName () +"()")); object[] Arguments=Joinpoint.getargs (); if(Arguments! =NULL&& arguments.length >0) { for(inti =0; i < arguments.length; i++) {Logger.error ("------->args["+ i +"]: "+jsonobject.tojsonstring (Arguments[i])); } } } Catch(Exception ex) {//Log the ground anomaly logsLogger.error ("-------> Exception Notification Exception"); Logger.error ("-------> Exception information: {}", Ex.getmessage ()); } }}
Log exception handling-spring AOP annotations