用Java实现一个清理自己项目的log日志

需求描述:

  • 每天凌晨3点清理7天前的日志文件
  • 日志文件清理定时任务

代码实现

package com.yang.job;

import com.hzys.base.config.AppConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.Arrays;
import java.util.List;

/**
 * 日志文件清理定时任务
 * 每天凌晨3点清理7天前的日志文件
 */
@Slf4j
@Component
public class LogCleanupJob {

    /**
     * 清理多少天前的文件
     */
    private static final int DAYS_BEFORE = 7;

    /**
     * 日志子目录列表
     */
    private static final List<String> LOG_SUB_DIRS = Arrays.asList("debug", "info", "warn", "error");

    // 从系统环境变量或JVM参数获取日志根路径,如果未设置则使用默认路径
    @Value("${JAR_DIR:${user.dir}}")
    private String jarDir;
    @Resource
    private AppConfig appConfig;

    /**
     * 每天凌晨3点执行日志清理
     * 清理规则:删除7天前修改的日志文件
     */
//    @Scheduled(cron = "0 */1 * * * ?")
    @Scheduled(cron = "0 0 3 * * ?")
    public void cleanupOldLogs() {
        String logRootPath = jarDir + File.separator + "logs";
        log.info("===== 开始执行日志文件清理任务 =====");
        log.info("日志根目录: {}", logRootPath);

        try {
            // 计算7天前的时间戳
            long sevenDaysAgo = System.currentTimeMillis() - (DAYS_BEFORE * 24 * 60 * 60 * 1000L);

            int totalDeleted = 0;
            int totalError = 0;

            // 清理主日志目录下的文件
            totalDeleted += cleanupDirectory(logRootPath, sevenDaysAgo, totalError);

            // 清理各子目录下的文件
            for (String subDir : LOG_SUB_DIRS) {
                String subDirPath = logRootPath + File.separator + subDir;
                if (appConfig.getIsDebug()) {
                    log.info("subDirPath is : {}", subDirPath);
                }
                totalDeleted += cleanupDirectory(subDirPath, sevenDaysAgo, totalError);
            }

            log.info("===== 日志文件清理任务完成,共删除 {} 个文件,删除失败 {} 个文件 =====", totalDeleted, totalError);
        } catch (Exception e) {
            log.error("执行日志文件清理任务时发生错误", e);
        }
    }

    /**
     * 清理指定目录下的过期文件
     *
     * @param directoryPath    目录路径
     * @param expireTimeMillis 过期时间戳(毫秒)
     * @param errorCount       错误计数器
     * @return 删除的文件数量
     */
    private int cleanupDirectory(String directoryPath, long expireTimeMillis, int errorCount) {
        int deletedCount = 0;

        File directory = new File(directoryPath);
        if (!directory.exists() || !directory.isDirectory()) {
            log.info("目录不存在或不是有效的目录: {}", directoryPath);
            return deletedCount;
        }

        File[] files = directory.listFiles();
        if (files == null) {
            log.info("无法获取目录下的文件列表: {}", directoryPath);
            return deletedCount;
        }

        for (File file : files) {
            if (file.isFile() && file.lastModified() < expireTimeMillis) {
                if (file.delete()) {
                    deletedCount++;
                    log.info("已删除过期日志文件: {}", file.getAbsolutePath());
                } else {
                    errorCount++;
                    log.info("删除过期日志文件失败: {}", file.getAbsolutePath());
                }
            }
        }

        return deletedCount;
    }
}




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值