[新增][定时任务]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,11 +1,14 @@
package cn.iocoder.yudao.framework.apilog.config;
import cn.iocoder.yudao.framework.apilog.core.filter.ApiAccessLogFilter;
import cn.iocoder.yudao.framework.apilog.core.job.ApiAccessLogJobHandler;
import cn.iocoder.yudao.framework.apilog.core.job.ApiErrorLogJobHandler;
import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService;
import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkServiceImpl;
import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService;
import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkServiceImpl;
import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum;
import cn.iocoder.yudao.framework.quartz.core.job.LogJobProperties;
import cn.iocoder.yudao.framework.web.config.WebProperties;
import cn.iocoder.yudao.framework.web.config.YudaoWebAutoConfiguration;
import cn.iocoder.yudao.module.infra.api.logger.ApiAccessLogApi;
@ -31,6 +34,16 @@ public class YudaoApiLogAutoConfiguration {
return new ApiErrorLogFrameworkServiceImpl(apiErrorLogApi);
}
@Bean
public ApiAccessLogJobHandler apiAccessLogJobHandler(LogJobProperties logJobProperties) {
return new ApiAccessLogJobHandler(logJobProperties);
}
@Bean
public ApiErrorLogJobHandler apiErrorLogJobHandler(LogJobProperties logJobProperties) {
return new ApiErrorLogJobHandler(logJobProperties);
}
/**
* 创建 ApiAccessLogFilter Bean记录 API 请求日志
*/

View File

@ -0,0 +1,34 @@
package cn.iocoder.yudao.framework.apilog.core.job;
import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.quartz.core.job.LogJobProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
/**
* @Author: j-sentinel
* @Date: 2023/9/30 16:13
*/
@Slf4j
@AllArgsConstructor
public class ApiAccessLogJobHandler implements JobHandler {
private LogJobProperties logJobProperties;
public ApiAccessLogJobHandler(LogJobProperties logJobProperties) {
this.logJobProperties = logJobProperties;
}
@Resource
private ApiAccessLogFrameworkService apiAccessLogFrameworkService;
@Override
public String execute(String param) throws Exception {
apiAccessLogFrameworkService.jobCleanAccessLog(logJobProperties.getAccessRetainDay());
return "";
}
}

View File

@ -0,0 +1,34 @@
package cn.iocoder.yudao.framework.apilog.core.job;
import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.quartz.core.job.LogJobProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
/**
* @Author: j-sentinel
* @Date: 2023/9/30 16:13
*/
@Slf4j
@AllArgsConstructor
public class ApiErrorLogJobHandler implements JobHandler {
private LogJobProperties logJobProperties;
public ApiErrorLogJobHandler(LogJobProperties logJobProperties) {
this.logJobProperties = logJobProperties;
}
@Resource
private ApiErrorLogFrameworkService apiErrorLogFrameworkService;
@Override
public String execute(String param) throws Exception {
apiErrorLogFrameworkService.jobCleanErrorLog(logJobProperties.getErrorRetainDay());
return "";
}
}

View File

@ -14,4 +14,10 @@ public interface ApiAccessLogFrameworkService {
*/
void createApiAccessLog(ApiAccessLog apiAccessLog);
/**
* 清理 @param accessLogJobDay 天的访问日志
*
* @param accessLogJobDay 超过多少天就进行清理
*/
void jobCleanAccessLog(Integer accessLogJobDay);
}

View File

@ -25,4 +25,9 @@ public class ApiAccessLogFrameworkServiceImpl implements ApiAccessLogFrameworkSe
apiAccessLogApi.createApiAccessLog(reqDTO);
}
@Override
public void jobCleanAccessLog(Integer accessLogJobDay) {
apiAccessLogApi.jobCleanAccessLog(accessLogJobDay);
}
}

View File

@ -14,4 +14,10 @@ public interface ApiErrorLogFrameworkService {
*/
void createApiErrorLog(ApiErrorLog apiErrorLog);
/**
* 清理 @param errorLogJobDay 天的访问日志
*
* @param errorLogJobDay 超过多少天就进行清理
*/
void jobCleanErrorLog(Integer errorLogJobDay);
}

View File

@ -25,4 +25,9 @@ public class ApiErrorLogFrameworkServiceImpl implements ApiErrorLogFrameworkServ
apiErrorLogApi.createApiErrorLog(reqDTO);
}
@Override
public void jobCleanErrorLog(Integer errorLogJobDay) {
apiErrorLogApi.jobCleanErrorLog(errorLogJobDay);
}
}