This method is AOP annotation type printing log, as for log insertion, this paper will not introduce.
The details are as follows:
Using the Gradle Guide Package:
compile 'org.springframework.boot:spring-boot-starter-aop
Customize an annotation class. The code is as follows:
package cn.cityworks.domain; import java.lang.annotation.*; /** * @author: zsx * @date: 2019/4/17 0017 * This is a custom annotation class. Used to specify that XX access can be logged */ @Target(ElementType.METHOD) //The target location of annotation placement. METHOD is the annotated METHOD level @Retention(RetentionPolicy.RUNTIME) //At which stage does annotation execute @Documented //Generate documents public @interface MyLog { String value() default ""; }
Add tangent method class. The code is as follows:
package cn.cityworks.utils; import javax.servlet.http.HttpServletRequest; import cn.cityworks.domain.MyLog; import org.aspectj.lang.JoinPoint; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.alibaba.fastjson.JSON; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import java.lang.reflect.Method; @Aspect @Component public class AspectLog { //Define Pointcut @ Pointcut //Cut in code at annotation location @Pointcut("@annotation( cn.cityworks.domain.MyLog)") public void logPoinCut() { } @AfterReturning("logPoinCut()") public void saveSysLog(JoinPoint join) { //Can be printed on the console Logger log = LoggerFactory.getLogger(AspectLog.class); //The method of obtaining the weaving point by reflection mechanism from the section weaving point MethodSignature signature = (MethodSignature) join.getSignature(); //How to get the pointcut Method method = signature.getMethod(); //Acquisition operation MyLog myLog = method.getAnnotation(MyLog.class); if (myLog != null) { String value = myLog.value(); log.info("Operation:"+value);//Save acquired operation } //Get the requested class name String className = join.getTarget().getClass().getName(); //Get the requested method name String methodName = method.getName(); log.info("Method name"+className + "." + methodName); //Requested parameters Object[] args = join.getArgs(); //Convert the array of parameters to json String params = JSON.toJSONString(args); log.info("params:{}"+params); ServletRequestAttributes attriButes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attriButes.getRequest(); //Print url log.info("url:{}", request.getRequestURL()); //Print request mode log.info("requet_method:{}", request.getMethod()); //Print class method log.info("class_method:{}", join.getStaticPart().getSignature().getDeclaringTypeName() + "." + join.getSignature().getName()); //Printing parameters log.info("parameter:{}", join.getArgs()); //Print Ip log.info("ip:{}", request.getLocalAddr()); } }
The preparations have been completed. Next, add notes to the corresponding Controller layer (methods without notes will not be printed).
Take login as an example, and add the annotation @ MyLog(value = "login"):
Results: