1、maven 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.43</version>
</dependency>
2、自定义注解:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface WebLog {
String describetion() default "";
}
3 、写切面类
package com.it.demo.aspect;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
@Slf4j
@Aspect
@Component
public class WebLogAspect {
private static final String LINE_SEPARATOR = System.lineSeparator();
@Pointcut("@annotation(com.it.demo.aspect.WebLog)")
public void webLog(){
}
@Around("webLog()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
long startTime = System.currentTimeMillis();
Object result = proceedingJoinPoint.proceed();
log.info("响应数据 : {}", JSONObject.toJSONString(result));
log.info("接口访问耗时 : {}",System.currentTimeMillis()-startTime);
return result;
}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable{
ServletRequestAttributes attributes =(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String aspectLogDescribetion = getAspectLogDescribetion(joinPoint);
log.info("========================================== Start ==========================================");
log.info("URL : {}", request.getRequestURL().toString());
log.info("Description : {}", aspectLogDescribetion);
log.info("HTTP Method : {}", request.getMethod());
log.info("Class Method : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
log.info("IP : {}", request.getRemoteAddr());
log.info("Request Args : {}", JSONObject.toJSONString(joinPoint.getArgs()));
}
@After("webLog()")
public void doAfter(JoinPoint joinPoint) throws Throwable{
log.info("===================================== end ====================================" + LINE_SEPARATOR);
}
public String getAspectLogDescribetion(JoinPoint joinPoint) throws Exception {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
StringBuilder description = new StringBuilder("");
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == arguments.length) {
description.append(method.getAnnotation(WebLog.class).describetion());
break;
}
}
}
return description.toString();
}
}
4、使用:
5、看效果