Files
javaSE1001/day02/src/com/inmind/student/黄越/LogServer.java
2025-12-21 17:24:54 +08:00

47 lines
2.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.inmind.student.黄越;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
public class LogServer {
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(8080)) {
System.out.println("LogServer started, listening on port 8080");
while (true) {
Socket socket = serverSocket.accept();
try (ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream())) {
Map<String, Long> levelCountMap = (Map<String, Long>) ois.readObject();
Map<String, Long> errorModuleCountMap = (Map<String, Long>) ois.readObject();
Log[] earliestLogs = (Log[]) ois.readObject();
System.out.println("1. 按级别统计:");
levelCountMap.forEach((level, count) -> {
String color = "ERROR".equals(level) ? "\u001B[43m" : ("WARNING".equals(level) ? "\u001B[43m" : "\u001B[37m");
System.out.println(color + level + ": " + count + "" + "\u001B[0m");
});
System.out.println("\n2. 按模块错误统计:");
errorModuleCountMap.forEach((module, count) -> System.out.println(module + ": " + count + "条错误"));
System.out.println("\n3. 最早的3条日志");
for (int i = 0; i < earliestLogs.length; i++) {
Log log = earliestLogs[i];
String color = i == 1 ? "\u001B[43m" : (i == 2 ? "\u001B[43m" : "\u001B[37m");
System.out.println(color + log + "\u001B[0m");
}
oos.writeObject("上报成功");
} catch (Exception e) {
System.out.println("Server error: " + e.getMessage());
}
}
} catch (Exception e) {
System.out.println("Server socket error: " + e.getMessage());
}
}
}