Message Log class-spring AOP

Source: Internet
Author: User
Tags erro getmessage throwable

  

 PackageCOM.APT.AOP;ImportJavax.annotation.Resource;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.aspectj.lang.JoinPoint;ImportOrg.aspectj.lang.ProceedingJoinPoint;Importorg.aspectj.lang.Signature;ImportOrg.aspectj.lang.annotation.Around;ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.aspectj.lang.annotation.Pointcut;Importorg.aspectj.lang.reflect.MethodSignature;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importorg.springframework.stereotype.Component;Importorg.springframework.web.context.request.RequestAttributes;ImportOrg.springframework.web.context.request.RequestContextHolder;Importorg.springframework.web.context.request.ServletRequestAttributes;ImportCom.alibaba.fastjson.JSONObject;Importcom.apt.dao.log.LogInfoRepository;ImportCom.apt.model.log.LogInfo;ImportCom.apt.util.ChkUtil;Importcom.apt.util.date.DateUtil, @Component @aspect Public classExceptionloghandler {Private Final StaticLogger Logger = Loggerfactory.getlogger (Exceptionloghandler.class); Private Final StaticString project_name = "AAA"; @ResourcePrivateloginforepository loginforepository; @Pointcut ("Execution (* com.apt.web). *.*(..))")     Public voidExceptionlog () {} @Around ("Exceptionlog ()")     PublicObject Around (joinpoint joinpoint) {loginfo loginfo=NewLoginfo ();        Loginfo.setprojectname (PROJECT_NAME); //map<string,object> Map = new hashmap<string,object> ();Jsonobject JSON =NewJsonobject (); Object reuslt=NULL; Try{String TargetName=joinpoint.gettarget (). GetClass (). GetName (); Signature Signature=joinpoint.getsignature (); Methodsignature methodsignature=(methodsignature) signature; String MethodName=Methodsignature.getname (); String[] Params=Methodsignature.getparameternames (); object[] Arguments=Joinpoint.getargs ();  for(inti=0; i<params.length; i++) {                if(Arguments[i]instanceofHttpServletRequest | | Arguments[i]instanceofHttpServletResponse)Continue;            Json.put (Params[i], arguments[i]);            } loginfo.setlogclass (TargetName);                        Loginfo.setlogmethod (MethodName); Requestattributes RA=requestcontextholder.getrequestattributes (); String IP= ""; if(RA! =NULL) {servletrequestattributes SRA=(servletrequestattributes) RA; HttpServletRequest Request=sra.getrequest (); String URL=Request.getrequesturl (). toString (); /*String method = Request.getmethod ();                String uri = Request.getrequesturi (); String queryString = request.getquerystring ();*/IP=request.getremoteaddr ();                Loginfo.setrequestip (IP);            Loginfo.setrequesturl (URL); }            /*Classpool pool = Classpool.getdefault ();              Classclasspath ClassPath = new Classclasspath (Joinpoint.gettarget (). GetClass ());              Pool.insertclasspath (ClassPath);              Ctclass cc = Pool.get (targetName);              Ctmethod cm = Cc.getdeclaredmethod (methodName);              MethodInfo MethodInfo = Cm.getmethodinfo ();              CodeAttribute CodeAttribute = Methodinfo.getcodeattribute ();             Localvariableattribute attr = (localvariableattribute) codeattribute.getattribute (Localvariableattribute.tag); if (attr = = null) {//exception} string[] Paramnames = new String[cm.getpar              Ametertypes (). length]; int pos = modifier.isstatic (Cm.getmodifiers ())?              0:1;            for (int i = 0; i < paramnames.length; i++) {Paramnames[i] = Attr.variablename (i + POS); } for (int i = 0; i < paramnames.length; i++) {if (Arguments[i] InstaNceof HttpServletRequest | |                Arguments[i] instanceof HttpServletResponse) continue;            Map.put (Paramnames[i], arguments[i]);  }*/Loginfo.setrequestparam (json.tostring ()); } Catch(Exception e) {logger.info (E.getmessage (), E);            Loginfo.seterrormsg (E.getmessage ());        E.fillinstacktrace (); }        Try{proceedingjoinpoint point=(proceedingjoinpoint) joinpoint; Reuslt=point.proceed (); if(!Chkutil.isempty (REUSLT))            {Loginfo.setresponseresult (reuslt.tostring ()); }        } Catch(Throwable e) {String msg=Errorclassinfo (e); Loginfo.seterrormsg (msg+e.fillinstacktrace (). GetMessage ()); }        Try{loginfo.setcreatetime (Dateutil.getcurrenttime ());        Loginforepository.save (Loginfo); }Catch(Exception e) {logger.info (E.getmessage (), E); }                returnreuslt; }            PrivateString Errorclassinfo (Throwable ex) {stacktraceelement[] Stacktraceelementarr=Ex.getstacktrace (); Stacktraceelement stacktraceelement= Stacktraceelementarr[0]; String msg= "Erro File:" +Stacktraceelement.getfilename (); Msg+ = "Erro Method:" +Stacktraceelement.getmethodname (); Msg+ = "Erro line:" +Stacktraceelement.getlinenumber (); returnmsg; }}

Message Log class-spring AOP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.