以下使用 Java 编程语言统计错误日志数量和占比的代码示例:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class LogAnalyzer {
    public static void main(String[] args) {
        String filePath = 'path/to/log/file.log';
        
        int totalLogs = 0;
        int errorLogs = 0;
        
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                totalLogs++;
                
                if (line.contains('logLev=[ERROR]')) {
                    errorLogs++;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        double errorPercentage = (double) errorLogs / totalLogs * 100;
        
        System.out.println('Total Logs: ' + totalLogs);
        System.out.println('Error Logs: ' + errorLogs);
        System.out.println('Error Log Percentage: ' + errorPercentage + '%');
    }
}

请将 'path/to/log/file.log' 替换为你的日志文件的实际路径。以上代码将统计日志文件中的总日志数量和错误日志数量,并计算错误日志占总日志数量的百分比。最后,将结果打印到控制台输出。

Java 统计日志文件错误日志数量和占比 - 代码示例

原文地址: https://www.cveoy.top/t/topic/hOxM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录