[新增][定时任务]ApiAccessLog、ApiErrorLog、JobLog 3个日志的定时清理

This commit is contained in:
j-sentinel
2023-10-01 16:53:17 +08:00
parent 3875c80471
commit 11921fab5a
29 changed files with 391 additions and 0 deletions

View File

@ -1,9 +1,12 @@
package cn.iocoder.yudao.framework.quartz.config;
import cn.iocoder.yudao.framework.quartz.core.job.JobLogJobHandler;
import cn.iocoder.yudao.framework.quartz.core.job.LogJobProperties;
import cn.iocoder.yudao.framework.quartz.core.scheduler.SchedulerManager;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
@ -15,6 +18,7 @@ import java.util.Optional;
@AutoConfiguration
@EnableScheduling // 开启 Spring 自带的定时任务
@Slf4j
@EnableConfigurationProperties(LogJobProperties.class)
public class YudaoQuartzAutoConfiguration {
@Bean
@ -26,4 +30,9 @@ public class YudaoQuartzAutoConfiguration {
return new SchedulerManager(scheduler.get());
}
@Bean
public JobLogJobHandler jobLogJobHandler(LogJobProperties logJobProperties){
return new JobLogJobHandler(logJobProperties);
}
}

View File

@ -0,0 +1,33 @@
package cn.iocoder.yudao.framework.quartz.core.job;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.quartz.core.service.JobLogFrameworkService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
/**
* @Author: j-sentinel
* @Date: 2023/9/30 20:40
*/
@Slf4j
@AllArgsConstructor
public class JobLogJobHandler implements JobHandler {
private LogJobProperties logJobProperties;
public JobLogJobHandler(LogJobProperties logJobProperties) {
this.logJobProperties = logJobProperties;
}
@Resource
private JobLogFrameworkService jobLogFrameworkService;
@Override
public String execute(String param) throws Exception {
Integer integer = jobLogFrameworkService.timingJobCleanLog(logJobProperties.getJobCleanRetainDay());
log.info("定时执行清理定时任务日志数量({})个",integer);
return String.format("定时执行清理定时任务日志数量 %s 个", integer);
}
}

View File

@ -0,0 +1,20 @@
package cn.iocoder.yudao.framework.quartz.core.job;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @Author: j-sentinel
* @Date: 2023/9/30 16:17
*/
@Data
@ConfigurationProperties(prefix = "yudao.clean-job")
public class LogJobProperties {
private int accessRetainDay = 7;
private int errorRetainDay = 8;
private int jobCleanRetainDay = 7;
}

View File

@ -41,4 +41,10 @@ public interface JobLogFrameworkService {
@NotNull(message = "运行时长不能为空") Integer duration,
boolean success, String result);
/**
* 清理 @param jobCleanRetainDay 天的访问日志
*
* @param jobCleanRetainDay 超过多少天就进行清理
*/
Integer timingJobCleanLog(Integer jobCleanRetainDay);
}