目录
-
POM依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
-
切面类
注:com.swhysc.rest..*(..)) 修改为自己的包名
import com.alibaba.fastjson.JSON;
import com.swhysc.exception.BizException;
import com.swhysc.vo.ResponseHandler;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
@Aspect
@Component
@Slf4j
public class AccessLogAspect {
/**
* The syntax of pointcut : https://blog.csdn.net/zhengchao1991/article/details/53391244
*/
@Pointcut("@annotation(AccessLog) && execution(public * com.swhysc.rest..*(..))")
public void pointcut() {
}
@Around(value = "pointcut()")
public Object around(ProceedingJoinPoint joinPoint) {
long startTime = System.currentTimeMillis();
logRequest(joinPoint);
Object response = null;
try {
response = joinPoint.proceed();
} catch (Throwable e) {
response = handleException(joinPoint, e);
} finally {
logResponse(startTime, response);
}
return response;
}
private Object handleException(ProceedingJoinPoint joinPoint, Throwable e) {
MethodSignature ms = (MethodSignature) joinPoint.getSignature();
Class returnType = ms.getReturnType();
log.warn("堆栈 EXCEPTION :", e);
if (e instanceof BizException) {
return ResponseHandler.handle(returnType, (BizException) e);
}
return ResponseHandler.handle(returnType, "UNKNOWN_ERROR", e.getMessage());
}
private void logResponse(long startTime, Object response) {
try {
long endTime = System.currentTimeMillis();
log.info("RESPONSE : {}", JSON.toJSONString(response));
log.info("@@@@@@ END PROCESSING COST : {} ms", (endTime - startTime));
} catch (Exception e) {
//swallow it
log.error("logResponse error : ", e);
}
}
private void logRequest(ProceedingJoinPoint joinPoint) {
try {
log.info("@@@@@@ START PROCESSING: {}", joinPoint.getSignature().toShortString());
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
log.info("REQUEST : {}", JSON.toJSONString(arg));
}
} catch (Exception e) {
//swallow it
log.error("logReqeust error : ", e);
}
}
}
-
注解类
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AccessLog {
}
-
restful接口类
import com.macow.log.aop.AccessLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class Hello {
@AccessLog
@GetMapping(value = "/hello")
public Object hello(String name) {
return "hello homelife";
}
}
-
验证: