In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Encapsulate the aop interception of the system's global operation log and package it to other project dependencies. In view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
In the process of development, in order to troubleshoot errors faster and better understand the number of interface visits, you can choose to use aop to intercept global operations. When there is more than one project, we can choose to be independent, and new projects can quickly add global operation logs. The specific codes and database tables are designed as follows:
1. Database MySQL table structure design, as shown in the following figure (fields can be added or subtracted as needed):
two。 The relevant entity classes and Mapper can be generated inversely according to the table structure, which is relatively simple (slightly)
3. Add global log aspect class
* * @ author songlonghui * @ ClassName SystemLogAspect * @ Description logging * @ date 16:38 on 2019-7-24 * @ Version 1.0 * / @ Component@Aspectpublic class SystemLogAspect {private Logger logger = LoggerFactory.getLogger (SystemLogAspect.class); @ Autowired private SystemLogDao systemLogDao; / * @ Value ("${LOG_POINT_URL}") private String logPointUrl * / / * * take all requests defined under the controller package as the entry point * / @ Pointcut ("execution (public * com.machinsight.*.*.controller..*.* (..)) & &! @ annotation (com.machinsight.system_log.core.annotation.NoAspectAnnotation)") public void webLog () {/ / logger.warn ("pointcut path->" + logPointUrl);} / / private SystemLogWithBLOBs systemLogWithBLOBs / * * before the pointcut, weave * @ param joinPoint * @ throws Throwable * / @ Before ("webLog ()") public void doBefore (JoinPoint joinPoint) throws Throwable {/ / start printing request log / / logger.info ("= = Start = =") } / * * weave * @ throws Throwable * / @ After ("webLog ()") public void doAfter () throws Throwable {logger.info ("= End = =") after the pointcut; / / leave a line of logger.info ("") between each request. } / * surround * @ param proceedingJoinPoint * @ return * @ throws Throwable * / @ Around ("webLog ()") public Object doAround (ProceedingJoinPoint proceedingJoinPoint) throws Throwable {/ / start time long startTime = System.currentTimeMillis (); SystemLogWithBLOBs systemLogWithBLOBs = new SystemLogWithBLOBs (); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes () If (null! = attributes) {HttpServletRequest request = attributes.getRequest (); logger.error ("current thread number:-- doAround--" + Thread.currentThread ()); String url = request.getRequestURL (). ToString (); String description = proceedingJoinPoint.getSignature (). GetDeclaringTypeName (); String requestArgs = FastJsonUtils.toJSONNoFeatures (proceedingJoinPoint.getArgs ()) .replaceAll ("\\", "") String ip = UuidUtil.getIpAddr (request); / / print request related parameters logger.info ("= = Start = ="); / / print request url logger.info ("request URL: {}", url) / print Http method logger.info ("HTTP request method Method: {}", request.getMethod ()); / / print the full path of calling controller and the execution method logger.info ("Class--Controller full path and execution method Method: {}. {}", description) / / print request IP logger.info ("request IP: {}", ip); / / print request input parameter logger.info ("request parameter Request Args: {}", requestArgs); / / record log entry String value = request.getHeader ("user-agent") / / user agent information / / systemLogWithBLOBs.setCreateTime (new Date ()); / / request parameter systemLogWithBLOBs.setMethod (url); / / API address systemLogWithBLOBs.setRequestIp (ip); / / request ip systemLogWithBLOBs.setRequestArgs (requestArgs); / / request parameter systemLogWithBLOBs.setUserAgent (value) / / user agent information systemLogWithBLOBs.setDescription (description);} Object result = null; try {result = proceedingJoinPoint.proceed (); String responseArgs = FastJsonUtils.toJSONNoFeatures (result) .replaceAll ("\", "); long useTime = System.currentTimeMillis ()-startTime / / print out parameter logger.info ("specific return parameters ResponseArgs: {}", responseArgs); / / execute logger.info ("overall execution time: {} ms", useTime); systemLogWithBLOBs.setResponseArgs (responseArgs); systemLogWithBLOBs.setTimeConsuming (Integer.valueOf (String.valueOf (useTime) } catch (Throwable throwable) {/ / set exception information systemLogWithBLOBs.setIsException (1); / / exception information systemLogWithBLOBs.setExceptionDetail (throwable.getMessage ()); / / time-consuming long exceptionTime = System.currentTimeMillis ()-startTime; systemLogWithBLOBs.setTimeConsuming (Integer.valueOf (String.valueOf (exceptionTime) / / exception return parameters JsonResult jsonResult = new JsonResult (); jsonResult.setMsg (CrmConstant.OPS_FAILED_MSG); jsonResult.setSuccess (CrmConstant.OPS_FAILED_CODE); jsonResult.setData (throwable.getMessage ()); String responseArgsForException = FastJsonUtils.toJSONNoFeatures (jsonResult); systemLogWithBLOBs.setResponseArgs (responseArgsForException) / / throw an exception throw new Throwable (throwable.getMessage ());} finally {systemLogDao.insertSelective (systemLogWithBLOBs);} return result;}
4. You can configure pointcuts according to the overall package structure of the company's project, and you can also add configurations that do not need to be intercepted. In order to be more flexible and detailed, corresponding annotation classes are added here, as follows:
/ * * exclude annotated classes * / @ Documented@Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.METHOD) public @ interface NoAspectAnnotation {}
5. As long as you add @ NoAspectAnnotation to the unwanted intercept controller method, the pointcut will be automatically excluded:
/ * @ author songlonghui * @ ClassName CommonController * @ Description TODO * @ date 16:09 on 2019-8-23 * @ Version 1.0 * / @ Controller@RequestMapping ("common") public class CommonController {@ ResponseBody @ RequestMapping ("/ test") @ NoAspectAnnotation public String testLog (@ RequestBody String inputJson) throws Exception {String msg = "Operation successful"; return msg;}}
6. As long as you add the project dependency to other projects:
Com.machinsight system_log 0.0.1-SNAPSHOT * *
Friends who need the source code of the whole project can contact me. I haven't decided where to put the source code for others to download!
Email address: lance911215@outlook.com
This is the answer to the question about encapsulating the aop interception of the system's global operation log and packaging it to other projects. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un