方式1:正则匹配

假设我当前的日志文件格式如下:

2024-02-29 20:57:11.129|INFO ||||69afe48a09964187a1b51192e1f9dae8||[http-nio-9527-exec-3]|c.z.a.config.LogAop - [afterReturning,119] - 
【<==request_id】:69afe48a09964187a1b51192e1f9dae8
【<==请求耗时】:9997毫秒

......此处省略一部分日志......

2024-02-29 20:57:19.310|INFO ||||65931ce2ec7f4cde9cf3b3ec19c8950e||[http-nio-9527-exec-4]|c.z.a.config.LogAop - [afterReturning,119] - 
【<==request_id】:65931ce2ec7f4cde9cf3b3ec19c8950e
【<==请求耗时】:293毫秒

......此处省略一部分日志......

Java代码如下:

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author : zanglikun
 * @date : 3/3/24 01:41
 * @desc : Copyright © zanglikun.com
 *
 * 我的日志文件中会包含:【<==请求耗时】:9997毫秒 这样的字符串,当前工具类统计每个日志文件中的请求耗时,并按照请求耗时从大到小排序输出。
 */
@Slf4j
public class LogFileCheckProcessRuntime {
    public static void main(String[] args) {
        String logDirectoryPath = "/Users/zanglikun/你的路径哦/";  // 替换为实际的日志文件目录路径,注意只会查找一级文件路径哦
        String keyword = "";  // 替换为你要匹配的关键词,可以使用空字符串匹配所有日志文件
        List<LogInfo> logInfoList = parseAndSortRequestTimesInDirectory(logDirectoryPath, keyword);
        for (LogInfo logInfo : logInfoList) {
            System.out.println("文件名: " + logInfo.getFileName() + ", 请求耗时: " + logInfo.getRequestTime() + " 毫秒");
        }
    }

    private static List<LogInfo> parseAndSortRequestTimesInDirectory(String logDirectoryPath, String keyword) {
        List<LogInfo> logInfoList = new ArrayList<>();
        File logDirectory = new File(logDirectoryPath);
        File[] logFiles = logDirectory.listFiles((dir, name) -> name.endsWith(".log") && name.contains(keyword));
        if (logFiles != null) {
            for (File logFile : logFiles) {
                List<Integer> requestTimes = parseRequestTimes(logFile);
                for (int time : requestTimes) {
                    logInfoList.add(new LogInfo(logFile.getName(), time));
                }
            }
        }
        logInfoList.sort(Collections.reverseOrder());
        return logInfoList;
    }

    private static List<Integer> parseRequestTimes(File logFile) {
        List<Integer> requestTimes = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(logFile))) {
            String line;
            Pattern pattern = Pattern.compile("【<==请求耗时】:(\\d+)毫秒");
            while ((line = br.readLine()) != null) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    int time = Integer.parseInt(matcher.group(1));
                    requestTimes.add(time);
                }
            }
        } catch (IOException e) {
            log.error("读取日志文件失败,错误信息如下:{}", ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }
        return requestTimes;
    }

    @Data
    static class LogInfo implements Comparable<LogInfo> {
        private String fileName;
        private int requestTime;

        public LogInfo(String fileName, int requestTime) {
            this.fileName = fileName;
            this.requestTime = requestTime;
        }

        public String getFileName() {
            return fileName;
        }

        public int getRequestTime() {
            return requestTime;
        }

        @Override
        public int compareTo(LogInfo other) {
            return Integer.compare(this.requestTime, other.requestTime);
        }
    }
}
特殊说明:
上述文章均是作者实际操作后产出。烦请各位,请勿直接盗用!转载记得标注原文链接:www.zanglikun.com
第三方平台不会及时更新本文最新内容。如果发现本文资料不全,可访问本人的Java博客搜索:标题关键字。以获取最新全部资料 ❤