Spring AOP records system logs

Source: Internet
Author: User

Configuration File: Xml Code <! -- Operation log section declaration --> <bean id = "logAspect" class = "com. tq365.service. sys. log. systemLogAspect "/> <aop: config> <aop: aspect ref =" logAspect "> </aop: aspect> </aop: config> implementation code: java code/*** System Operation Log plane ** @ author archie2010 * since 02:44:03 */@ Aspect public class SystemLogAspect {// The Class of int and long is automatically converted into its encapsulation class private static final String integerClazz = "class java. lang. integer "; private static fi Nal String longClazz = "class java. lang. long "; @ Resource private SystemLogService systemLogService; private Logger logger = Logger. getLogger (this. getClass (). getName (); @ Pointcut ("execution (* com. tq365.service .. *. *(..)) ") public void myAspect () {}; @ AfterThrowing (pointcut =" myAspect () ", throwing =" e ") public void doAfterThrowing (JoinPoint jp, Throwable e) {System. out. println ("exception:" + e. getMes Sage (); System. out. println (e. getClass (). getName (); System. out. println ("exception class:" + jp. getTarget (). getClass (). getName (); System. out. println ("" + jp. getSignature (). getName () + "method throw exception"); // logger. error ("error! Error level !!! "+ E. getMessage (); logger. error ("Oops =" + jp. getTarget (). getClass (). "+ jp. In getName () +. getSignature (). getName () + "method throw" + e. getClass (). getName () + "exception"); System. out. println ("parameter:"); if (jp. getArgs ()! = Null & jp. getArgs (). length> 0) {for (int I = 0; I <jp. getArgs (). length; I ++) {System. out. println (jp. getArgs () [I]. toString (); logger. error ("parameter: --" + jp. getArgs () [I]. toString () ;}}@ SuppressWarnings ("unchecked") @ After ("@ annotation (com. tq365.sys. annotation. systemLogAnnotation) ") public void doAfter (JoinPoint jp) {System. out. println ("---------- post notification"); System. out. println ("method class:" + jp. getT Arget (). getClass (). getName (); System. out. println ("" + jp. getSignature (). getName () + "method"); String methodName = jp. getSignature (). getName (); // operation log object ----------------- SystemLog sysLog = new SystemLog (); // operation parameter ----------------- String descArgs = "parameter"; if (jp. getArgs ()! = Null & jp. getArgs (). length> 0) {for (int I = 0; I <jp. getArgs (). length; I ++) {if (jp. getArgs () [I]! = Null) {// System. out. println (jp. getArgs () [I]. toString (); descArgs + = jp. getArgs () [I]. toString () + "," ;}else {descArgs + = "null" + "," ;}} System. out. println ("------ parameter" + descArgs);} sysLog. setOperateArgs (descArgs); String des = null; // method description if (! (MethodName. startsWith ("set") | methodName. startsWith ("get") {Class targetClass = jp. getTarget (). getClass (); // The method does not target the Clazz parameter... class [] claszs = new Class [jp. getArgs (). length]; for (int I = 0; I <jp. getArgs (). length; I ++) {// System. out. println (jp. getArgs () [I]); if (jp. getArgs () [I]! = Null) {System. out. println (jp. getArgs () [I]. getClass (); if (jp. getArgs () [I]. getClass (). toString (). equals (integerClazz) {claszs [I] = int. class;} else if (jp. getArgs () [I]. getClass (). toString (). equals (longClazz) {claszs [I] = long. class;} else {claszs [I] = jp. getArgs () [I]. getClass () ;}} else if (jp. getArgs () [I] = null) {claszs [I] = String. class ;}} Method method = null; try {method = targetClass. get Method (methodName, claszs);} catch (SecurityException e) {} catch (NoSuchMethodException e) {}// if the Method is empty (the description cannot be obtained, des = null) if (method! = Null) {System. out. println (method. getAnnotation (SystemLogAnnotation. class ). description (); des = method. getAnnotation (SystemLogAnnotation. class ). description () ;}// obtain the Session HttpSession session = ServletActionContext. getRequest (). getSession (); // get the current User appUser = (User) session. getAttribute ("USER"); if (appUser! = Null) {System. out. println ("the user already exists in the Session"); // operation log object sysLog. setUid (appUser. getUserId (); sysLog. setUsername (appUser. getFullName ();} HttpServletRequest request = ServletActionContext. getRequest (); String ip = request. getRemoteAddr (); sysLog. setOperateTime (DateUtil. getCurrentTime (); sysLog. setOperateDes (methodName + "->" + des); sysLog. setIp (ip); systemLogService. save (sysLog); System. out. println ("---------- save operation log ");}}

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.