package com.tencent.common.aoplog;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by developer on 2021/12/9.
*/
//只有定义为 RetentionPolicy.RUNTIME(在运行时有效)时,我们才能通过反射获取到注解,然后根据注解的一系列值,变更不同的操作。
@Target(ElementType.METHOD)
// 指定生效至运行时
@Retention(RetentionPolicy.RUNTIME)
public @interface AOPLog {
/**
* 指定是否详情显示
* true 显示详情, 默认false
*
* @return
*/
boolean isDetail() default false;
}
package com.tencent.common.aoplog;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
//指定切面类
@Aspect
// 注入容器
@Component
public class AOPLogAspect {
private static Logger log = LoggerFactory.getLogger(AOPLogAspect.class);
/**
* 指定切点, 切点的位置是存在该注解com.tencent.common.aoplog.AOPLog
*/
@Pointcut("@annotation(com.tencent.common.aoplog.AOPLog)")
public void logPointCut() {
}
/**
* 环绕通知, 该处写具体日志逻辑
*
* @param joinPoint
*/
@Around("logPointCut()")
public void logAround(ProceedingJoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
// 获取方法名称
String methodName = signature.getName();
// 获取入参
Object[] param = joinPoint.getArgs();
StringBuilder sb = new StringBuilder();
for (Object o : param) {
sb.append(o).append("; ");
}
log.info("进入方法[{}], 参数有[{}]", methodName, sb.toString());
String resp = "";
try {
Object proceed = joinPoint.proceed();
resp = JSON.toJSONString(proceed, SerializerFeature.WriteMapNullValue);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
// 获取方法上的注解,判断如果isDetail值为true,则打印结束日志
Method method = signature.getMethod();
AOPLog annotation = method.getAnnotation(AOPLog.class);
boolean isDetail = annotation.isDetail();
if (isDetail) {
log.info("执行结束[{}], 返回值[{}]", methodName, resp);
}
}
}
package com.tencent.pc.controller;
import com.tencent.common.aoplog.AOPLog;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author developer
* @date 2019/12/26
* @description //TODO
*/
@RestController
public class DemoController {
// 指定注解@AOPLog
@AOPLog(isDetail = true)
@GetMapping("/demo0")
public String demo0(String name,String age) {
return "name"+name+"age"+age ;
}
// 指定注解@AOPLog, 同时isDetail = true
//http://127.0.0.1:8080/demo1?name=xiaomi
@AOPLog(isDetail = true)
@GetMapping("/demo1")
public String demo1(String name) {
return "{ \"name\":\"xiaomi\" , \"age\":\"10\" },";
}
}
2021-12-09 15:52:51.989 INFO 14140 --- [nio-8080-exec-3] com.tencent.common.aoplog.AOPLogAspect : 进入方法[demo1], 参数有[xiaomi; ]
2021-12-09 15:52:51.989 INFO 14140 --- [nio-8080-exec-3] com.tencent.common.aoplog.AOPLogAspect : 执行结束[demo1], 返回值["{ \"name\":\"xiaomi\" , \"age\":\"10\" },"]