自定义注解实现方法级别的日志管理
测试结果截图
以上结果展示了通过注解获取Controller请求地址,方法参数,方法返回结果,方法名,方法类路径,参数名称,参数内容,以及注解相关信息
创建注解类@Log
BusinessType:拓展类,可以自主添加或者保留想要的信息
public enum BusinessType {
OTHER,
INSERT,
UPDATE,
DELETE,
IMPORT,
EXPORT,
}
Log
@Target({ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {
// 标题
String title() default "";
// 设置一个业务类型(新增,删除...)
BusinessType businessType() default BusinessType.OTHER;
// 是否保存请求参数
boolean isSaveRequestData() default true;
}
创建切面类LogAspect
LogAspect
@Aspect
@Component
public class LogAspect {
private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
@Autowired
private SecurityUtil securityUtil;
/***
* 配置织入点
*/
@Pointcut("@annotation(com.example.bootactiviti7.aspect.log.Log)")
public void logPointCut() {
}
@AfterReturning(pointcut = "logPointCut()", returning = "jsonResult")
public void doAfterReturning(JoinPoint joinPoint, Object jsonResult) {
handle(joinPoint, null, jsonResult);
}
@AfterThrowing(pointcut = "logPointCut()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
handle(joinPoint, e, null);
}
private void handle(JoinPoint joinPoint, Exception e, Object jsonResult) {
Log annotationLog = getAnnotationLog(joinPoint);
if (Objects.isNull(annotationLog)) {
return;
}
// 获取当前登录用户
//securityUtil.logInAs("system");
//Long userId = 100L;
// TODO:这里就是看需要将你想要的日志存储起来的地方,就是组装好你的日志类,然后insert进数据库,每个人的实现方法不一样,也很简单,就不赘述了,如有需要帮助,请留言讨论
// 返回信息或者异常信息
log.info("jsonResult====== " + JSON.toJSONString(jsonResult));
if (Objects.nonNull(e)) {
log.info("Exception ====== " + e.getMessage());
}
// 请求方法
log.info("methodName ====== " + joinPoint.getSignature().getName());
log.info("className ====== " + joinPoint.getTarget().getClass().getName());
// 请求参数
log.info("parameter ====== " + Arrays.toString(joinPoint.getArgs()));
log.info("joinPoint ====== " + Arrays.toString(((MethodSignature) joinPoint.getSignature()).getParameterNames()));
// 打印注解上的东西
log.info("title ====== " + annotationLog.title());
log.info("businessType().ordinal ====== " + annotationLog.businessType().ordinal());
log.info("annotationLog.isSaveRequestData ====== " + annotationLog.isSaveRequestData());
// 打印请求地址
log.info("Request URL ===== " + ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest().getRequestURI());
}
/***
判断方法是否存在Log注解,并且返回Log包含的信息
*/
public Log getAnnotationLog(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null) {
return method.getAnnotation(Log.class);
}
return null;
}
}
创建测试LogController
@RestController
public class LogController {
@GetMapping("/testLog")
@Log(title = "测试删除日志", businessType = BusinessType.DELETE)
public String testLogTag(String name, Integer age){
return "测试删除日志返回结果";
}
}
之后启动项目,访问你的项目地址localhost:xxxx/testLog?name=aaa&age=18然后看你的日志消息,就可以看见第一张图的结果啦!