add: acs添加定时删除日志文件功能,修改叫空逻辑不需要区域

This commit is contained in:
yanps
2024-09-29 11:01:01 +08:00
parent 962338862e
commit 31dea7a46d
9 changed files with 339 additions and 21 deletions

View File

@@ -0,0 +1,71 @@
package org.nl.quartz.task;
import cn.hutool.core.util.ObjectUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.nl.config.SpringContextHolder;
import org.nl.system.service.param.dao.Param;
import org.nl.system.service.param.impl.SysParamServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.concurrent.TimeUnit;
/**
* @author LENOVO
* 定时清理日志文件
*/
@Slf4j
@Component
@Profile("prod")
public class AutoCleanFile {
private static long DAYS_TO_MILLIS = TimeUnit.DAYS.toMillis(14);
@Value("${logging.file.path}")
private String logPath;
@SneakyThrows
public void run() {
log.info("定时清理日志文件AutoCleanFile开始:");
File directory = new File(logPath);
if (directory.exists() && directory.isDirectory()) {
cleanDirectory(directory);
} else {
System.out.println("指定的路径不存在或不是一个目录。");
}
}
// 递归清理文件夹内的过期文件
private static void cleanDirectory(File directory) {
File[] files = directory.listFiles();
if (files != null) {
long currentTime = System.currentTimeMillis();
for (File file : files) {
if (file.isDirectory()) {
// 如果是目录,递归调用
cleanDirectory(file);
} else if (file.isFile()) {
SysParamServiceImpl sysParamService = SpringContextHolder.getBean(SysParamServiceImpl.class);
Param byCode = sysParamService.findByCode("log_time");
if (ObjectUtil.isNotNull(byCode)) {
DAYS_TO_MILLIS = TimeUnit.DAYS.toMillis(Long.parseLong(byCode.getValue()));
}
if (currentTime - file.lastModified() > DAYS_TO_MILLIS) {
boolean deleted = file.delete();
if (deleted) {
log.info("已删除文件: " + file.getAbsolutePath());
} else {
log.info("无法删除文件: " + file.getAbsolutePath());
}
} else {
log.info("文件未超过七天, 不做任何修改: " + file.getAbsolutePath());
}
}
}
}
}
}