Commit 00c2153fd306169b4962c0a0ddc04ea208cfab91

Authored by 芋道源码
Committed by Gitee
2 parents 3875c804 11921fab

!646 ApiAccessLog、ApiErrorLog、JobLog 3 个日志的 定时 物理清理

Merge pull request !646 from lanxindx/feature/mall_product
Showing 29 changed files with 391 additions and 0 deletions
yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoQuartzAutoConfiguration.java
1 1 package cn.iocoder.yudao.framework.quartz.config;
2 2  
  3 +import cn.iocoder.yudao.framework.quartz.core.job.JobLogJobHandler;
  4 +import cn.iocoder.yudao.framework.quartz.core.job.LogJobProperties;
3 5 import cn.iocoder.yudao.framework.quartz.core.scheduler.SchedulerManager;
4 6 import lombok.extern.slf4j.Slf4j;
5 7 import org.quartz.Scheduler;
6 8 import org.springframework.boot.autoconfigure.AutoConfiguration;
  9 +import org.springframework.boot.context.properties.EnableConfigurationProperties;
7 10 import org.springframework.context.annotation.Bean;
8 11 import org.springframework.scheduling.annotation.EnableScheduling;
9 12  
... ... @@ -15,6 +18,7 @@ import java.util.Optional;
15 18 @AutoConfiguration
16 19 @EnableScheduling // 开启 Spring 自带的定时任务
17 20 @Slf4j
  21 +@EnableConfigurationProperties(LogJobProperties.class)
18 22 public class YudaoQuartzAutoConfiguration {
19 23  
20 24 @Bean
... ... @@ -26,4 +30,9 @@ public class YudaoQuartzAutoConfiguration {
26 30 return new SchedulerManager(scheduler.get());
27 31 }
28 32  
  33 + @Bean
  34 + public JobLogJobHandler jobLogJobHandler(LogJobProperties logJobProperties){
  35 + return new JobLogJobHandler(logJobProperties);
  36 + }
  37 +
29 38 }
... ...
yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/job/JobLogJobHandler.java 0 → 100644
  1 +package cn.iocoder.yudao.framework.quartz.core.job;
  2 +
  3 +import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
  4 +import cn.iocoder.yudao.framework.quartz.core.service.JobLogFrameworkService;
  5 +import lombok.AllArgsConstructor;
  6 +import lombok.extern.slf4j.Slf4j;
  7 +
  8 +import javax.annotation.Resource;
  9 +
  10 +/**
  11 + * @Author: j-sentinel
  12 + * @Date: 2023/9/30 20:40
  13 + */
  14 +@Slf4j
  15 +@AllArgsConstructor
  16 +public class JobLogJobHandler implements JobHandler {
  17 +
  18 + private LogJobProperties logJobProperties;
  19 +
  20 + public JobLogJobHandler(LogJobProperties logJobProperties) {
  21 + this.logJobProperties = logJobProperties;
  22 + }
  23 +
  24 + @Resource
  25 + private JobLogFrameworkService jobLogFrameworkService;
  26 +
  27 + @Override
  28 + public String execute(String param) throws Exception {
  29 + Integer integer = jobLogFrameworkService.timingJobCleanLog(logJobProperties.getJobCleanRetainDay());
  30 + log.info("定时执行清理定时任务日志数量({})个",integer);
  31 + return String.format("定时执行清理定时任务日志数量 %s 个", integer);
  32 + }
  33 +}
... ...
yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/job/LogJobProperties.java 0 → 100644
  1 +package cn.iocoder.yudao.framework.quartz.core.job;
  2 +
  3 +import lombok.Data;
  4 +import org.springframework.boot.context.properties.ConfigurationProperties;
  5 +
  6 +/**
  7 + * @Author: j-sentinel
  8 + * @Date: 2023/9/30 16:17
  9 + */
  10 +@Data
  11 +@ConfigurationProperties(prefix = "yudao.clean-job")
  12 +public class LogJobProperties {
  13 +
  14 + private int accessRetainDay = 7;
  15 +
  16 + private int errorRetainDay = 8;
  17 +
  18 + private int jobCleanRetainDay = 7;
  19 +
  20 +}
... ...
yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/service/JobLogFrameworkService.java
... ... @@ -41,4 +41,10 @@ public interface JobLogFrameworkService {
41 41 @NotNull(message = "运行时长不能为空") Integer duration,
42 42 boolean success, String result);
43 43  
  44 + /**
  45 + * 清理 @param jobCleanRetainDay 天的访问日志
  46 + *
  47 + * @param jobCleanRetainDay 超过多少天就进行清理
  48 + */
  49 + Integer timingJobCleanLog(Integer jobCleanRetainDay);
44 50 }
... ...
yudao-framework/yudao-spring-boot-starter-web/pom.xml
... ... @@ -61,6 +61,12 @@
61 61 <artifactId>jsoup</artifactId>
62 62 </dependency>
63 63  
  64 + <!-- Job 定时任务相关 -->
  65 + <dependency>
  66 + <groupId>cn.iocoder.boot</groupId>
  67 + <artifactId>yudao-spring-boot-starter-job</artifactId>
  68 + </dependency>
  69 +
64 70 </dependencies>
65 71  
66 72 </project>
... ...
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/config/YudaoApiLogAutoConfiguration.java
1 1 package cn.iocoder.yudao.framework.apilog.config;
2 2  
3 3 import cn.iocoder.yudao.framework.apilog.core.filter.ApiAccessLogFilter;
  4 +import cn.iocoder.yudao.framework.apilog.core.job.ApiAccessLogJobHandler;
  5 +import cn.iocoder.yudao.framework.apilog.core.job.ApiErrorLogJobHandler;
4 6 import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService;
5 7 import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkServiceImpl;
6 8 import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService;
7 9 import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkServiceImpl;
8 10 import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum;
  11 +import cn.iocoder.yudao.framework.quartz.core.job.LogJobProperties;
9 12 import cn.iocoder.yudao.framework.web.config.WebProperties;
10 13 import cn.iocoder.yudao.framework.web.config.YudaoWebAutoConfiguration;
11 14 import cn.iocoder.yudao.module.infra.api.logger.ApiAccessLogApi;
... ... @@ -31,6 +34,16 @@ public class YudaoApiLogAutoConfiguration {
31 34 return new ApiErrorLogFrameworkServiceImpl(apiErrorLogApi);
32 35 }
33 36  
  37 + @Bean
  38 + public ApiAccessLogJobHandler apiAccessLogJobHandler(LogJobProperties logJobProperties) {
  39 + return new ApiAccessLogJobHandler(logJobProperties);
  40 + }
  41 +
  42 + @Bean
  43 + public ApiErrorLogJobHandler apiErrorLogJobHandler(LogJobProperties logJobProperties) {
  44 + return new ApiErrorLogJobHandler(logJobProperties);
  45 + }
  46 +
34 47 /**
35 48 * 创建 ApiAccessLogFilter Bean,记录 API 请求日志
36 49 */
... ...
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/job/ApiAccessLogJobHandler.java 0 → 100644
  1 +package cn.iocoder.yudao.framework.apilog.core.job;
  2 +
  3 +import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService;
  4 +import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
  5 +import cn.iocoder.yudao.framework.quartz.core.job.LogJobProperties;
  6 +import lombok.AllArgsConstructor;
  7 +import lombok.extern.slf4j.Slf4j;
  8 +
  9 +import javax.annotation.Resource;
  10 +
  11 +/**
  12 + * @Author: j-sentinel
  13 + * @Date: 2023/9/30 16:13
  14 + */
  15 +@Slf4j
  16 +@AllArgsConstructor
  17 +public class ApiAccessLogJobHandler implements JobHandler {
  18 +
  19 + private LogJobProperties logJobProperties;
  20 +
  21 + public ApiAccessLogJobHandler(LogJobProperties logJobProperties) {
  22 + this.logJobProperties = logJobProperties;
  23 + }
  24 +
  25 + @Resource
  26 + private ApiAccessLogFrameworkService apiAccessLogFrameworkService;
  27 +
  28 + @Override
  29 + public String execute(String param) throws Exception {
  30 + apiAccessLogFrameworkService.jobCleanAccessLog(logJobProperties.getAccessRetainDay());
  31 + return "";
  32 + }
  33 +
  34 +}
... ...
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/job/ApiErrorLogJobHandler.java 0 → 100644
  1 +package cn.iocoder.yudao.framework.apilog.core.job;
  2 +
  3 +import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService;
  4 +import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
  5 +import cn.iocoder.yudao.framework.quartz.core.job.LogJobProperties;
  6 +import lombok.AllArgsConstructor;
  7 +import lombok.extern.slf4j.Slf4j;
  8 +
  9 +import javax.annotation.Resource;
  10 +
  11 +/**
  12 + * @Author: j-sentinel
  13 + * @Date: 2023/9/30 16:13
  14 + */
  15 +@Slf4j
  16 +@AllArgsConstructor
  17 +public class ApiErrorLogJobHandler implements JobHandler {
  18 +
  19 + private LogJobProperties logJobProperties;
  20 +
  21 + public ApiErrorLogJobHandler(LogJobProperties logJobProperties) {
  22 + this.logJobProperties = logJobProperties;
  23 + }
  24 +
  25 + @Resource
  26 + private ApiErrorLogFrameworkService apiErrorLogFrameworkService;
  27 +
  28 + @Override
  29 + public String execute(String param) throws Exception {
  30 + apiErrorLogFrameworkService.jobCleanErrorLog(logJobProperties.getErrorRetainDay());
  31 + return "";
  32 + }
  33 +
  34 +}
... ...
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkService.java
... ... @@ -14,4 +14,10 @@ public interface ApiAccessLogFrameworkService {
14 14 */
15 15 void createApiAccessLog(ApiAccessLog apiAccessLog);
16 16  
  17 + /**
  18 + * 清理 @param accessLogJobDay 天的访问日志
  19 + *
  20 + * @param accessLogJobDay 超过多少天就进行清理
  21 + */
  22 + void jobCleanAccessLog(Integer accessLogJobDay);
17 23 }
... ...
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkServiceImpl.java
... ... @@ -25,4 +25,9 @@ public class ApiAccessLogFrameworkServiceImpl implements ApiAccessLogFrameworkSe
25 25 apiAccessLogApi.createApiAccessLog(reqDTO);
26 26 }
27 27  
  28 + @Override
  29 + public void jobCleanAccessLog(Integer accessLogJobDay) {
  30 + apiAccessLogApi.jobCleanAccessLog(accessLogJobDay);
  31 + }
  32 +
28 33 }
... ...
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkService.java
... ... @@ -14,4 +14,10 @@ public interface ApiErrorLogFrameworkService {
14 14 */
15 15 void createApiErrorLog(ApiErrorLog apiErrorLog);
16 16  
  17 + /**
  18 + * 清理 @param errorLogJobDay 天的访问日志
  19 + *
  20 + * @param errorLogJobDay 超过多少天就进行清理
  21 + */
  22 + void jobCleanErrorLog(Integer errorLogJobDay);
17 23 }
... ...
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkServiceImpl.java
... ... @@ -25,4 +25,9 @@ public class ApiErrorLogFrameworkServiceImpl implements ApiErrorLogFrameworkServ
25 25 apiErrorLogApi.createApiErrorLog(reqDTO);
26 26 }
27 27  
  28 + @Override
  29 + public void jobCleanErrorLog(Integer errorLogJobDay) {
  30 + apiErrorLogApi.jobCleanErrorLog(errorLogJobDay);
  31 + }
  32 +
28 33 }
... ...
yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiAccessLogApi.java
... ... @@ -18,4 +18,10 @@ public interface ApiAccessLogApi {
18 18 */
19 19 void createApiAccessLog(@Valid ApiAccessLogCreateReqDTO createDTO);
20 20  
  21 + /**
  22 + * 清理 @param accessLogJobDay 天的访问日志
  23 + *
  24 + * @param accessLogJobDay 超过多少天就进行清理
  25 + */
  26 + void jobCleanAccessLog(Integer accessLogJobDay);
21 27 }
... ...
yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiErrorLogApi.java
... ... @@ -18,4 +18,10 @@ public interface ApiErrorLogApi {
18 18 */
19 19 void createApiErrorLog(@Valid ApiErrorLogCreateReqDTO createDTO);
20 20  
  21 + /**
  22 + * 清理 @param errorLogJobDay 天的访问日志
  23 + *
  24 + * @param errorLogJobDay 超过多少天就进行清理
  25 + */
  26 + void jobCleanErrorLog(Integer errorLogJobDay);
21 27 }
... ...
yudao-module-infra/yudao-module-infra-biz/pom.xml
... ... @@ -35,6 +35,10 @@
35 35 <groupId>cn.iocoder.boot</groupId>
36 36 <artifactId>yudao-spring-boot-starter-biz-operatelog</artifactId>
37 37 </dependency>
  38 + <dependency>
  39 + <groupId>cn.iocoder.boot</groupId>
  40 + <artifactId>yudao-spring-boot-starter-biz-tenant</artifactId>
  41 + </dependency>
38 42  
39 43 <!-- Web 相关 -->
40 44 <dependency>
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiAccessLogApiImpl.java
... ... @@ -24,4 +24,9 @@ public class ApiAccessLogApiImpl implements ApiAccessLogApi {
24 24 apiAccessLogService.createApiAccessLog(createDTO);
25 25 }
26 26  
  27 + @Override
  28 + public void jobCleanAccessLog(Integer accessLogJobDay) {
  29 + apiAccessLogService.jobCleanAccessLog(accessLogJobDay);
  30 + }
  31 +
27 32 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiErrorLogApiImpl.java
... ... @@ -24,4 +24,9 @@ public class ApiErrorLogApiImpl implements ApiErrorLogApi {
24 24 apiErrorLogService.createApiErrorLog(createDTO);
25 25 }
26 26  
  27 + @Override
  28 + public void jobCleanErrorLog(Integer errorLogJobDay) {
  29 + apiErrorLogService.jobCleanErrorLog(errorLogJobDay);
  30 + }
  31 +
27 32 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/job/JobLogMapper.java
... ... @@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogExportReq
7 7 import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogPageReqVO;
8 8 import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobLogDO;
9 9 import org.apache.ibatis.annotations.Mapper;
  10 +import org.apache.ibatis.annotations.Param;
10 11  
11 12 import java.util.List;
12 13  
... ... @@ -40,4 +41,7 @@ public interface JobLogMapper extends BaseMapperX&lt;JobLogDO&gt; {
40 41 );
41 42 }
42 43  
  44 + Integer timingJobCleanLog(@Param("jobCleanRetainDay") Integer jobCleanRetainDay);
  45 +
  46 + void optimizeTable();
43 47 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/logger/ApiAccessLogMapper.java
... ... @@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.Api
7 7 import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO;
8 8 import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiAccessLogDO;
9 9 import org.apache.ibatis.annotations.Mapper;
  10 +import org.apache.ibatis.annotations.Param;
10 11  
11 12 import java.util.List;
12 13  
... ... @@ -44,4 +45,7 @@ public interface ApiAccessLogMapper extends BaseMapperX&lt;ApiAccessLogDO&gt; {
44 45 );
45 46 }
46 47  
  48 + Integer jobCleanAccessLog(@Param("accessLogJobDay") Integer accessLogJobDay);
  49 +
  50 + void optimizeTable();
47 51 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/logger/ApiErrorLogMapper.java
... ... @@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiE
7 7 import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO;
8 8 import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO;
9 9 import org.apache.ibatis.annotations.Mapper;
  10 +import org.apache.ibatis.annotations.Param;
10 11  
11 12 import java.util.List;
12 13  
... ... @@ -42,4 +43,7 @@ public interface ApiErrorLogMapper extends BaseMapperX&lt;ApiErrorLogDO&gt; {
42 43 );
43 44 }
44 45  
  46 + Integer jobCleanErrorLog(@Param("errorLogJobDay") Integer errorLogJobDay);
  47 +
  48 + void optimizeTable();
45 49 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobLogServiceImpl.java
... ... @@ -26,6 +26,8 @@ import java.util.List;
26 26 @Slf4j
27 27 public class JobLogServiceImpl implements JobLogService {
28 28  
  29 + private static final Integer DELETE_LIMIT = 1;
  30 +
29 31 @Resource
30 32 private JobLogMapper jobLogMapper;
31 33  
... ... @@ -51,6 +53,22 @@ public class JobLogServiceImpl implements JobLogService {
51 53 }
52 54  
53 55 @Override
  56 + public Integer timingJobCleanLog(Integer jobCleanRetainDay) {
  57 + Integer result = null;
  58 + int count = 0;
  59 + while (result == null || DELETE_LIMIT.equals(result)){
  60 + result = jobLogMapper.timingJobCleanLog(jobCleanRetainDay);
  61 + count += result;
  62 + }
  63 + if(count > 0){
  64 + // ALTER TABLE...FORCE 会导致表重建发生,这会根据主键索引对表空间中的物理页进行排序。
  65 + // 它将行压缩到页面上并消除可用空间,同时确保数据处于主键查找的最佳顺序。
  66 + jobLogMapper.optimizeTable();
  67 + }
  68 + return count;
  69 + }
  70 +
  71 + @Override
54 72 public JobLogDO getJobLog(Long id) {
55 73 return jobLogMapper.selectById(id);
56 74 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogService.java
... ... @@ -38,4 +38,10 @@ public interface ApiAccessLogService {
38 38 */
39 39 List<ApiAccessLogDO> getApiAccessLogList(ApiAccessLogExportReqVO exportReqVO);
40 40  
  41 + /**
  42 + * 清理 @param accessLogJobDay 天的访问日志
  43 + *
  44 + * @param accessLogJobDay 超过多少天就进行清理
  45 + */
  46 + void jobCleanAccessLog(Integer accessLogJobDay);
41 47 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiAccessLogServiceImpl.java
1 1 package cn.iocoder.yudao.module.infra.service.logger;
2 2  
3 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
  4 +import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
4 5 import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO;
5 6 import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogExportReqVO;
6 7 import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO;
7 8 import cn.iocoder.yudao.module.infra.convert.logger.ApiAccessLogConvert;
8 9 import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiAccessLogDO;
9 10 import cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiAccessLogMapper;
  11 +import lombok.extern.slf4j.Slf4j;
10 12 import org.springframework.stereotype.Service;
11 13 import org.springframework.validation.annotation.Validated;
12 14  
... ... @@ -18,10 +20,13 @@ import java.util.List;
18 20 *
19 21 * @author 芋道源码
20 22 */
  23 +@Slf4j
21 24 @Service
22 25 @Validated
23 26 public class ApiAccessLogServiceImpl implements ApiAccessLogService {
24 27  
  28 + private static final Integer DELETE_LIMIT = 100;
  29 +
25 30 @Resource
26 31 private ApiAccessLogMapper apiAccessLogMapper;
27 32  
... ... @@ -41,4 +46,22 @@ public class ApiAccessLogServiceImpl implements ApiAccessLogService {
41 46 return apiAccessLogMapper.selectList(exportReqVO);
42 47 }
43 48  
  49 + @Override
  50 + public void jobCleanAccessLog(Integer accessLogJobDay) {
  51 + TenantUtils.executeIgnore(() -> {
  52 + Integer result = null;
  53 + int count = 0;
  54 + while (result == null || DELETE_LIMIT.equals(result)) {
  55 + result = apiAccessLogMapper.jobCleanAccessLog(accessLogJobDay);
  56 + count += result;
  57 + }
  58 + if (count > 0) {
  59 + // ALTER TABLE...FORCE 会导致表重建发生,这会根据主键索引对表空间中的物理页进行排序。
  60 + // 它将行压缩到页面上并消除可用空间,同时确保数据处于主键查找的最佳顺序。
  61 + apiAccessLogMapper.optimizeTable();
  62 + }
  63 + log.info("定时执行清理访问日志数量({})个", count);
  64 + });
  65 + }
  66 +
44 67 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogService.java
... ... @@ -47,4 +47,10 @@ public interface ApiErrorLogService {
47 47 */
48 48 void updateApiErrorLogProcess(Long id, Integer processStatus, Long processUserId);
49 49  
  50 + /**
  51 + * 清理 @param errorLogJobDay 天的访问日志
  52 + *
  53 + * @param errorLogJobDay 超过多少天就进行清理
  54 + */
  55 + void jobCleanErrorLog(Integer errorLogJobDay);
50 56 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/logger/ApiErrorLogServiceImpl.java
1 1 package cn.iocoder.yudao.module.infra.service.logger;
2 2  
3 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
  4 +import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
4 5 import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO;
5 6 import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogExportReqVO;
6 7 import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO;
... ... @@ -8,6 +9,7 @@ import cn.iocoder.yudao.module.infra.convert.logger.ApiErrorLogConvert;
8 9 import cn.iocoder.yudao.module.infra.dal.dataobject.logger.ApiErrorLogDO;
9 10 import cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiErrorLogMapper;
10 11 import cn.iocoder.yudao.module.infra.enums.logger.ApiErrorLogProcessStatusEnum;
  12 +import lombok.extern.slf4j.Slf4j;
11 13 import org.springframework.stereotype.Service;
12 14 import org.springframework.validation.annotation.Validated;
13 15  
... ... @@ -24,10 +26,13 @@ import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_L
24 26 *
25 27 * @author 芋道源码
26 28 */
  29 +@Slf4j
27 30 @Service
28 31 @Validated
29 32 public class ApiErrorLogServiceImpl implements ApiErrorLogService {
30 33  
  34 + private static final Integer DELETE_LIMIT = 100;
  35 +
31 36 @Resource
32 37 private ApiErrorLogMapper apiErrorLogMapper;
33 38  
... ... @@ -62,4 +67,22 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService {
62 67 .processUserId(processUserId).processTime(LocalDateTime.now()).build());
63 68 }
64 69  
  70 + @Override
  71 + public void jobCleanErrorLog(Integer errorLogJobDay) {
  72 + TenantUtils.executeIgnore(() -> {
  73 + Integer result = null;
  74 + int count = 0;
  75 + while (result == null || DELETE_LIMIT.equals(result)) {
  76 + result = apiErrorLogMapper.jobCleanErrorLog(errorLogJobDay);
  77 + count += result;
  78 + }
  79 + if (count > 0) {
  80 + // ALTER TABLE...FORCE 会导致表重建发生,这会根据主键索引对表空间中的物理页进行排序。
  81 + // 它将行压缩到页面上并消除可用空间,同时确保数据处于主键查找的最佳顺序。
  82 + apiErrorLogMapper.optimizeTable();
  83 + }
  84 + log.info("定时执行清理错误日志数量({})个",count);
  85 + });
  86 + }
  87 +
65 88 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/mapper/ApiAccessLogMapper.xml 0 → 100644
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3 +<mapper namespace="cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiAccessLogMapper">
  4 +
  5 + <!--
  6 + 一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
  7 + 无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
  8 + 代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
  9 + 文档可见:https://www.iocoder.cn/MyBatis/x-plugins/
  10 + -->
  11 +
  12 + <delete id="jobCleanAccessLog" parameterType="integer">
  13 + DELETE FROM infra_api_access_log
  14 + WHERE id IN (
  15 + SELECT * FROM (
  16 + SELECT id FROM infra_api_access_log
  17 + WHERE create_time &lt; DATE_SUB(CURDATE(), INTERVAL #{accessLogJobDay} DAY)
  18 + ORDER BY id ASC LIMIT 100
  19 + ) AS a
  20 + )
  21 + </delete>
  22 +
  23 + <select id="optimizeTable">
  24 + ALTER TABLE infra_job_log FORCE
  25 + /*
  26 + ALTER TABLE infra_job_log ENGINE = INNODB,
  27 + ALGORITHM = INPLACE,
  28 + LOCK = NONE
  29 + */
  30 + </select>
  31 +
  32 +</mapper>
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/mapper/ApiErrorLogMapper.xml 0 → 100644
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3 +<mapper namespace="cn.iocoder.yudao.module.infra.dal.mysql.logger.ApiErrorLogMapper">
  4 +
  5 + <!--
  6 + 一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
  7 + 无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
  8 + 代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
  9 + 文档可见:https://www.iocoder.cn/MyBatis/x-plugins/
  10 + -->
  11 +
  12 + <delete id="jobCleanErrorLog" parameterType="integer">
  13 + DELETE FROM infra_api_error_log
  14 + WHERE id IN (
  15 + SELECT * FROM (
  16 + SELECT id FROM infra_api_error_log
  17 + WHERE create_time &lt; DATE_SUB(CURDATE(), INTERVAL #{errorLogJobDay} DAY)
  18 + ORDER BY id ASC LIMIT 100
  19 + ) AS a
  20 + )
  21 + </delete>
  22 +
  23 + <select id="optimizeTable">
  24 + ALTER TABLE infra_api_error_log FORCE
  25 + /*
  26 + ALTER TABLE infra_api_error_log ENGINE = INNODB,
  27 + ALGORITHM = INPLACE,
  28 + LOCK = NONE
  29 + */
  30 + </select>
  31 +
  32 +</mapper>
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/mapper/JobLogMapper.xml 0 → 100644
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3 +<mapper namespace="cn.iocoder.yudao.module.infra.dal.mysql.job.JobLogMapper">
  4 +
  5 + <!--
  6 + 一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
  7 + 无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
  8 + 代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
  9 + 文档可见:https://www.iocoder.cn/MyBatis/x-plugins/
  10 + -->
  11 +
  12 + <delete id="timingJobCleanLog" parameterType="integer">
  13 + DELETE FROM infra_job_log
  14 + WHERE id IN (
  15 + SELECT * FROM (
  16 + SELECT id FROM infra_job_log
  17 + WHERE create_time &lt; DATE_SUB(CURDATE(), INTERVAL #{jobCleanRetainDay} DAY)
  18 + ORDER BY id ASC LIMIT 100
  19 + ) AS a
  20 + )
  21 + </delete>
  22 +
  23 + <select id="optimizeTable">
  24 + ALTER TABLE infra_job_log FORCE
  25 + /*
  26 + ALTER TABLE infra_job_log ENGINE = INNODB,
  27 + ALGORITHM = INPLACE,
  28 + LOCK = NONE
  29 + */
  30 + </select>
  31 +
  32 +</mapper>
... ...
yudao-server/src/main/resources/application-local.yaml
... ... @@ -213,6 +213,10 @@ yudao:
213 213 error-code: # 错误码相关配置项
214 214 enable: false
215 215 demo: false # 关闭演示模式
  216 + clean-job:
  217 + access-retain-day: 7
  218 + error-retain-day: 8
  219 + job-clean-retain-day: 8
216 220  
217 221 justauth:
218 222 enabled: true
... ...