操作日志别在业务代码里一行行写qkl-boot 用 AOP 注解把这件事收口了导读“谁在什么时候把岗位下架了”“谁改了这个职位的薪资”这种问题做招聘系统基本每天都会被运营问一次。一开始我在业务代码里手动记日志结果每个接口都要写一遍样式还不统一。后来改成 AOP 切面 自定义注解业务代码一行日志都不用写想记录哪个接口就给哪个接口加个注解。这篇讲具体实现。第一步定义注解先定义一个OperLog注解用来声明这个接口需要记录操作日志Target(ElementType.METHOD)Retention(RetentionPolicy.RUNTIME)publicinterfaceOperLog{/** 模块名比如职位管理 */Stringmodule()default;/** 操作类型ADD/UPDATE/DELETE/QUERY */Stringtype()default;/** 操作描述 */Stringdesc()default;}用法很简单在 Controller 方法上加注解就行RestControllerRequestMapping(/job)publicclassJobController{OperLog(module职位管理,typeUPDATE,desc下架职位)PostMapping(/offline)publicResultoffline(RequestParamLongjobId){jobService.offline(jobId);returnResult.ok();}}第二步写切面统一收集信息切面里做三件事取注解信息、取当前登录用户、记录操作内容AspectComponentpublicclassOperLogAspect{AutowiredprivateOperLogServiceoperLogService;Around(annotation(com.qkl.boot.annotation.OperLog))publicObjectaround(ProceedingJoinPointpjp)throwsThrowable{longstartSystem.currentTimeMillis();Objectresultpjp.proceed();longcostSystem.currentTimeMillis()-start;// 1. 取注解上的模块和描述MethodSignaturesignature(MethodSignature)pjp.getSignature();Methodmethodsignature.getMethod();OperLogoperLogmethod.getAnnotation(OperLog.class);// 2. 取当前登录用户Sa-Token 里拿自己项目里换成自己的登录态StpUseruserStpUtil.getSession().getModel(user,StpUser.class);// 3. 组装日志对象异步落库见第三步OperLogEntityentitynewOperLogEntity();entity.setUserId(user.getId());entity.setUsername(user.getUsername());entity.setModule(operLog.module());entity.setType(operLog.type());entity.setDesc(operLog.desc());entity.setParams(JSON.toJSONString(pjp.getArgs()));entity.setCost(cost);entity.setCreateTime(LocalDateTime.now());operLogService.saveAsync(entity);returnresult;}}这里有个细节方法执行成功才记日志如果接口抛异常了proceed()会直接抛出去后面的日志组装不会执行——异常场景我在异常切面里单独记录两个切面职责分开。第三步异步落库别拖慢主流程日志查询不是核心链路同步写库会拖慢接口响应。我用 Spring 的异步线程池来写// OperLogServiceServicepublicclassOperLogService{AutowiredprivateOperLogMapperoperLogMapper;Async(operLogExecutor)publicvoidsaveAsync(OperLogEntityentity){operLogMapper.insert(entity);}}线程池单独配置避免和业务线程池互相干扰ConfigurationpublicclassAsyncConfig{Bean(operLogExecutor)publicThreadPoolTaskExecutoroperLogExecutor(){ThreadPoolTaskExecutorexecutornewThreadPoolTaskExecutor();executor.setCorePoolSize(2);executor.setMaxPoolSize(4);executor.setQueueCapacity(1000);executor.setThreadNamePrefix(oper-log-);executor.setRejectedExecutionHandler(newThreadPoolExecutor.CallerRunsPolicy());executor.initialize();returnexecutor;}}队列满了以后走 CallerRunsPolicy由调用线程自己写日志不丢。踩坑方法自调用切面根本没生效现象给某个 Service 内部的方法加了OperLog前端调接口后日志表里一条都没有。单测直接调方法也没日志。排查一开始以为是切面没扫描到检查了Aspect和启动类EnableAspectJAutoProxy都在。然后想到一个常见的坑——同一个类里方法自己调自己不走代理。看代码发现接口是jobService.offline(jobId)但offline内部又调了同一个类里的另一个OperLog方法那个方法被 this 调用代理对象根本没介入。定位Spring AOP 是基于代理的this.xxx()调用不会经过代理所以注解不生效。这不是配置问题是调用方式问题。解决把需要记录日志的逻辑抽到独立 Bean或者用AopContext.currentProxy()拿代理对象再调用ServicepublicclassJobServiceImpl{OperLog(module职位管理,typeUPDATE,desc下架职位)Overridepublicvoidoffline(LongjobId){// 内部需要再次经过代理的场景用 AopContext 拿代理((JobService)AopContext.currentProxy()).doOffline(jobId);}publicvoiddoOffline(LongjobId){// 真正的下架逻辑}}需要EnableAspectJAutoProxy(exposeProxy true)才能用 AopContext。可直接复用的清单注解 切面收口操作日志业务代码零侵入日志异步落库线程池独立配置队列满用 CallerRunsPolicy 兜底不丢日志方法自调用不走代理切面不生效——要么拆 Bean要么用 AopContext.currentProxy()异常场景单独在异常切面记录正常/异常日志职责分开记录参数时注意脱敏密码、token 别直接序列化进日志这套日志方案在 qkl-boot 里落地后运营查谁动了什么数据都是查表就行再也没人翻代码找日志了。核心就一句话切面能做的事别让业务代码重复造轮子。项目源码https://gitee.com/gzqkl/qkl-boot