Appearance
AOP 实现日志打印
自定义注解
java
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Log {
/**
* 模块名称
*/
String module() default "";
/**
* 具体操作
*/
String desc() default "";
}定义切面
java
@Aspect
@Component
@Slf4j
public class LogAspect {
@Pointcut("@annotation(operationLog)")
private void recordLog(Log operationLog) {
}
@Around("recordLog(operationLog)")
public Object logAround(ProceedingJoinPoint pjp, Log operationLog) throws
Throwable {
String module = operationLog.module();
String desc = operationLog.desc();
long uid = System.nanoTime();
String threadName = Thread.currentThread().getName();
log.info("{} - 【{}】 模块: {}, 操作: {}, 请求参数: {}",
threadName, uid, module, desc, Arrays.toString(pjp.getArgs()));
Object ret = pjp.proceed();
log.info("{} - 【{}】 返回值: {}", threadName, uid, ret);
return ret;
}
}Controller 测试
java
@Log(module = "认证模块", desc = "用户登录")
@PostMapping("/login")
public String login(@RequestBody LoginDTO loginDTO, @RequestParam(value = "remember", required = false) boolean rememberMe) {
return authService.login(loginDTO);
}输出结果

更新: 2025-07-17 17:38:36
原文: https://www.yuque.com/lsxxyg/sz/vpx0m9yr6o6dfy1r