106 lines
4.2 KiB
Java
106 lines
4.2 KiB
Java
package com.inmind.student.罗康凯;
|
|
|
|
|
|
import java.io.*;
|
|
import java.net.*;
|
|
import java.util.*;
|
|
import java.util.concurrent.*;
|
|
import java.util.regex.*;
|
|
import java.util.stream.*;
|
|
|
|
public class LogAnalyzer {
|
|
private static final String LOG_DIR = "C:\\Users\\30914\\IdeaProjects\\WebProject\\summerday\\S_day13\\src\\testkaoshi";
|
|
private static final String SERVER_HOST = "localhost";
|
|
private static final int SERVER_PORT = 8080;
|
|
private static final Pattern LOG_PATTERN = Pattern.compile("^(\\d+),(\\w+),(\\w+),(.*)$");
|
|
|
|
private static final List<Log> logs = Collections.synchronizedList(new ArrayList<>());
|
|
private static final ExecutorService executor = Executors.newFixedThreadPool(5);
|
|
|
|
public static void main(String[] args) {
|
|
try {
|
|
// 1. 读取并解析日志文件
|
|
File[] logFiles = new File(LOG_DIR).listFiles(f -> f.getName().endsWith(".log"));
|
|
if (logFiles == null || logFiles.length == 0) {
|
|
System.out.println("未找到日志文件");
|
|
return;
|
|
}
|
|
|
|
// 2. 多线程处理日志文件
|
|
List<Future<?>> futures = Arrays.stream(logFiles)
|
|
.map(file -> executor.submit(() -> processLogFile(file)))
|
|
.collect(Collectors.toList());
|
|
|
|
// 等待所有任务完成
|
|
for (Future<?> future : futures) {
|
|
future.get();
|
|
}
|
|
|
|
// 3. 统计分析
|
|
Map<String, Long> levelCount = logs.stream()
|
|
.collect(Collectors.groupingBy(Log::getLevel, Collectors.counting()));
|
|
|
|
Map<String, Long> moduleErrorCount = logs.stream()
|
|
.filter(log -> "ERROR".equals(log.getLevel()))
|
|
// .collect(Collectors.groupingBy(Log::getModule, Collectors.counting()));
|
|
.collect(Collectors.groupingBy(l->l.getModule(), Collectors.counting()));
|
|
|
|
List<Log> earliestLogs = logs.stream()
|
|
.sorted()
|
|
.limit(3)
|
|
.collect(Collectors.toList());
|
|
|
|
// 4. 网络上报
|
|
sendResults(levelCount, moduleErrorCount, earliestLogs);
|
|
|
|
} catch (Exception e) {
|
|
System.err.println("处理过程中发生错误: " + e.getMessage());
|
|
} finally {
|
|
executor.shutdown();
|
|
}
|
|
}
|
|
|
|
private static void processLogFile(File file) {
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
|
String line;
|
|
while ((line = reader.readLine()) != null) {
|
|
try {
|
|
Matcher matcher = LOG_PATTERN.matcher(line.trim());
|
|
if (matcher.matches()) {
|
|
long timestamp = Long.parseLong(matcher.group(1));
|
|
String level = matcher.group(2);
|
|
String module = matcher.group(3);
|
|
String content = matcher.group(4);
|
|
logs.add(new Log(timestamp, level, module, content));
|
|
}
|
|
} catch (Exception e) {
|
|
System.err.println("解析日志行失败: " + line + ", 错误: " + e.getMessage());
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
System.err.println("读取文件失败: " + file.getName() + ", 错误: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
private static void sendResults(Map<String, Long> levelCount,
|
|
Map<String, Long> moduleErrorCount,
|
|
List<Log> earliestLogs) {
|
|
try (Socket socket = new Socket(SERVER_HOST, SERVER_PORT);
|
|
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
|
|
|
|
// 发送统计结果
|
|
out.writeObject(levelCount);
|
|
out.writeObject(moduleErrorCount);
|
|
out.writeObject(earliestLogs);
|
|
out.flush();
|
|
|
|
// 接收服务器响应
|
|
String response = in.readLine();
|
|
System.out.println("服务器响应: " + response);
|
|
|
|
} catch (IOException e) {
|
|
System.err.println("网络通信失败: " + e.getMessage());
|
|
}
|
|
}
|
|
} |