96SEO 2026-09-05 07:54 3
一套完善的操作日志程序几乎是标配。
但日志记录最怕两件事:

在看代码之前,先理解整条数据流:
业务方法被调用
↓@AuditLog 注解标记 → AOP 切面拦截
↓主线程的观点是,收集请求上下文、使用者信息、入参 → 建立日志 VO
从↓主线程来看,执行目标方法
再看↓主线程,记录耗时、执行结果、异常信息
↓异步线程池的观点是。序列化 JSON → 推入 Redis List
再看↓定时任务,批量从 Redis 取出 → 反序列化 → 批量写入 MySQL
主要设计思想是 三层解耦
采集层: 零侵入地收集日志信息,不污染业务代码;
缓冲层: 削峰填谷。日志写入 Redis 是微秒级操作,几乎不影响主流程;
持久层: 批量入库。降低数据库写入压力,支持重试和降级。
import java.lang.annotation.*;其实,@Target
@Retention
@Documented
public @interface AuditLog {
/** 操作模块名称 */
String firstModule default "";怎么说呢,/** 操作模块名称 */
String secondModule default "";/** 操作类型 */
String operationType;/** 操作描述 */
String operationDesc default "";}
This annotation's sole responsibility is to declaratively describe "what this operation is". It decouples logging logic from business code and makes it runtime-accessible for AOP.
Add one line on controller method。zero intrusion:
@AuditLog(
firstModule = SysAuditLogEnum.FIRST_LEVEL_BASIC_SET,secondModule = SysAuditLogEnum.SECOND_LEVEL_BASE_MANAGE,operationType = SysAuditLogEnum.ADD,operationDesc = "新增基地")
@PostMapping
public ServerResponse create BaseVO baseVO) {
return owmsBaseService.add;}
@Aspect
@Component
@Slf4j
public class AuditLogAspect {
private static final String AUDIT_LOG_QUEUE = "audit_log_queue";private static final String SUCCESS_STATUS = "success";其实,private static final String FAIL_STATUS = "fail";@Resource
private ThreadPoolTaskExecutor auditLogExecutor;怎么说呢,@Resource
private RedisUtil redisUtil;@Around")
public Object around throws Throwable {
log.info;其实,// 获取请求上下文
ServletRequestAttributes attributes =
RequestContextHolder.getRequestAttributes;if ) {
log.warn;return joinPoint.proceed;// 必须放行目标方法
}
HttpServletRequest request = attributes.getRequest;ThreadSession threadSession = ThreadSession.getThreadSession;// 建立基础信息
AuditLogRecordVO auditLogRecordVO =
buildAuditLog;StopWatch stopWatch = new StopWatch;话说回来,stopWatch.start;Object result = null;String status = SUCCESS_STATUS;String errorMsg = null;try {
result = joinPoint.proceed;话说回来,} catch {
status = FAIL_STATUS;errorMsg = e.getMessage;throw e,// 抛出异常保持业务原有行为
} finally {
stopWatch.stop;auditLogRecordVO.setExecutionTime);auditLogRecordVO.setStatus;auditLogRecordVO.setErrorMessage;// 异步写 Redis
auditLogExecutor.execute -> {
try {
String auditJson = JSONUtil.toJsonStr;log.info,redisUtil.lRightPush;老实说,} catch {
log.error;}
}),}
return result;}
private AuditLogRecordVO buildAuditLog(HttpServletRequest request,ThreadSession threadSession。AuditLog auditlog,ProceedingJoinPoint joinPoint) {
// 注解属性
String firstModule = auditlog.firstModule;String secondModule = auditlog.secondModule;String operationDesc= auditlog.operationDesc;String operationType= auditlog.operationType;// 方法参数
Object args = joinPoint.getArgs;String operationContext= JSONUtil.toJsonStr;
说起来,// 使用者信息从自定义 Session 中获取
String operatorAccount= threadSession.getLoginName;String operatorName = threadSession.getFiled;...
return AuditLogRecordVO.builder
.operatorAccount
.operatorName
.operationType
.operationDesc
.operationContext
... /* 省略其余字段赋值 */
.build;}
}
Around advice captures before/after/exception in one place: get request context & annotations before proceeding;record time & status after execution;finally push serialized log into Redis asynchronously.
@Around 能同时拿到参数、返回值与异常。并控制 proceed 的时机,是测算耗时所必需的唯一选择。
No matter success or exception,finally executes. Exception sets status='fail'。records message n rethrows – business behavior untouched but log remains complete.
User info & parameters are available early;time & result are only known post-execution – keeps logic tidy.
| 场景 | 同步写 Redis | 异步线程池 | |||||
|---|---|---|---|---|---|---|---|
| Redis 正常 | 接口 RT +1ms | 主线程 +0ms | |||||
| Redis 抖动 | 接口 RT +50ms | 主线程 +0ms | |||||
| Redis 宕机 | 接口阻塞至超时 | 主线程立即返回 |
| 参数 | 取值逻辑 | 说明 |
|---|---|---|
| corePoolSize | CPU 核数 | I/O 主导,可按核数设定 |
| maxPoolSize | CPU × MULTIPLE | 突发流量临时扩容 |
| queueCapacity | max × MULTIPLE | 缓冲队列避免过载 |
| keepAliveSeconds | 30s | 非主要空闲后回收 |
| RejectedExecutionHandler | CallerRunsPolicy | 队列满时退化为同步执行保证不丢 |
CallerRunsPolicy 是关键选择。相较 Abort/Discard。它会让调用者直接跑任务,实现限流降速,而不是丢失。
提交方式
java
auditExecutor.execute -> {
try {
redisUtil.lRightPush(AUDIT_LOG_QUEUE,JSONUtil.toJsonStr);说起来,}catch{
log.error;}
}),
使用 execute 而非 submit,因为无需 Future 返回。
if){
redisUtil.lTrim,-1);//原子裁剪
for{
try{logs.add);其实,}catch{log.error;}
}
if){
service.saveBatch;//一次 DB 写多个 INSERT 合并成一次交互。}
}
}catch{log.error;}
}
**关键点**
* lRange+ lTrim 一次性读 & 裁剪减少网络往返。其实,* 每条 JSON 单独捕获错误;局部失败不致全局,说起来,* saveBatch 批量 DB 极大减少压力。
│ 业务方法 │
│ @Audit Log+PostMapping │
└───────▲──────────────────┘
│
▼
┌───────▼─────────────────────┐
│ AOP 切面拦截 │
│①采集注解属性 │
│②采集请求上下文 │
│③采集使用者信息 │
│④执行目标方法 │
│⑤记录耗时 & 状态 │
└───────▲─────────────────────┘
│
▼
┌───────▼─────────────────────┐
│ 异步线程池 |
│ 序列化JSON→Redis List │
│ “auditlogqueue” │
└───────▲──────────────────────┘
│
▼
┌───────▼─────────────────────┐
│ 定时使用者 │
│ 每 X 量拉取 N 条 │
│ 反序列化→saveBatch→MySQL │
└──────────────────────────────┘
三个层面各司其职: • AOP 切面负责“采集”——零侵入声明式;• Redis 队列负责“缓冲”——削峰填谷;其实,• 定时使用者负责“持久化”——批量存储。怎么说呢,
一套好的操作日志方案应该像空气一样存在——开发者只需要在方法上加一个注解。其余全部自动完成,怎么说呢,这篇文章介绍的 @AuditLog + AOP + 异步 + Redis + 定时批量存储方案正是这一理念落地的典范。按理说,在实际项目中,你可以根据自己的技术栈与规模细节进行微调。但主要思路保持不变即可轻松解决侵入性与性能瓶颈这两大痛点。
作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。
| 服务项目 | 基础套餐 | 标准套餐 | 高级定制 |
|---|---|---|---|
| 关键词优化数量 | 10-20个核心词 | 30-50个核心词+长尾词 | 80-150个全方位覆盖 |
| 内容优化 | 基础页面优化 | 全站内容优化+每月5篇原创 | 个性化内容策略+每月15篇原创 |
| 技术SEO | 基本技术检查 | 全面技术优化+移动适配 | 深度技术重构+性能优化 |
| 外链建设 | 每月5-10条 | 每月20-30条高质量外链 | 每月50+条多渠道外链 |
| 数据报告 | 月度基础报告 | 双周详细报告+分析 | 每周深度报告+策略调整 |
| 效果保障 | 3-6个月见效 | 2-4个月见效 | 1-3个月快速见效 |
我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:
全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。
基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。
解决网站技术问题,优化网站结构,提升页面速度和移动端体验。
创作高质量原创内容,优化现有页面,建立内容更新机制。
获取高质量外部链接,建立品牌在线影响力,提升网站权威度。
持续监控排名、流量和转化数据,根据效果调整优化策略。
基于我们服务的客户数据统计,平均优化效果如下:
我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。
Demand feedback