init
This commit is contained in:
35
day02/src/com/inmind/student/张洁/Log.java
Normal file
35
day02/src/com/inmind/student/张洁/Log.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package com.inmind.student.张洁;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Log implements Serializable {
|
||||
private long timetamp;
|
||||
private String level;
|
||||
private String module;
|
||||
private String content;
|
||||
|
||||
public Log(long timetamp, String level, String module, String content) {
|
||||
this.timetamp = timetamp;
|
||||
this.level = level;
|
||||
this.module = module;
|
||||
this.content = content;
|
||||
}
|
||||
public long getTimetamp() {
|
||||
return timetamp;
|
||||
}
|
||||
public String getLevel() {
|
||||
return level;
|
||||
}
|
||||
public String getModule() {
|
||||
return module;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "["+timetamp+"] "+level+" "+module+": "+content;
|
||||
}
|
||||
}
|
||||
151
day02/src/com/inmind/student/张洁/LogAnalyzer.java
Normal file
151
day02/src/com/inmind/student/张洁/LogAnalyzer.java
Normal file
@@ -0,0 +1,151 @@
|
||||
package com.inmind.student.张洁;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
|
||||
public class LogAnalyzer {
|
||||
public static final String LOG_DIR = "D:/workspace_idea/JavaSE202507/NextDay13_test/logs/";
|
||||
private static final String serverAdd = "localhost";
|
||||
private static final int port = 8080;
|
||||
|
||||
private List<Log> logs = new ArrayList<Log>();
|
||||
|
||||
private static final Object LOCK = new Object();
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
if (!Files.exists(Paths.get(LOG_DIR))) {
|
||||
throw new FileNotFoundException("<EFBFBD><EFBFBD>־Ŀ¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" + LOG_DIR);
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// <20><>ȡ<EFBFBD><C8A1>־<EFBFBD>ļ<EFBFBD><C4BC>б<EFBFBD>
|
||||
File logFile = new File(LOG_DIR);
|
||||
File[] files = logFile.listFiles((dir, name) -> name.endsWith(".log"));
|
||||
if (files == null) {
|
||||
System.out.println("δ<EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD>־<EFBFBD>ļ<EFBFBD>");
|
||||
return;
|
||||
}
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
|
||||
List<Thread> threads = new ArrayList<>();
|
||||
LogAnalyzer analyzer = new LogAnalyzer();
|
||||
for (File file : files) {
|
||||
Thread thread = new Thread(() -> analyzer.parseLogFile(file));
|
||||
threads.add(thread);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
for (Thread thread : threads) {
|
||||
try {
|
||||
thread.join();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
LogAnalyzer analyzer2= new LogAnalyzer();
|
||||
Map<String, Integer> levelmap = analyzer2.levelmapCount();
|
||||
Map<String, Integer> moduleErrorMap = analyzer2.moduleErrorCount();
|
||||
List<Log> earlistLogs = analyzer2.earlistLogsCount();
|
||||
analyzer2.sendToServer(levelmap, moduleErrorMap, earlistLogs);
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>Ǿ<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD>
|
||||
private void parseLogFile(File file) {
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
try {
|
||||
Log log = parseLogLine(line);
|
||||
synchronized (LOCK) {
|
||||
logs.add(log);
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
System.out.println("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD>ļ<EFBFBD> " + file.getName() + " <20><><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>" + line + "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>" + e.getMessage());
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static Log parseLogLine(String line) {
|
||||
String[] parts = line.split(",", 4);
|
||||
if (parts.length != 4) {
|
||||
throw new IllegalArgumentException("<EFBFBD><EFBFBD>־<EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ ʱ<><CAB1><EFBFBD>,<2C><><EFBFBD><EFBFBD>,ģ<><C4A3>,<2C><><EFBFBD><EFBFBD>");
|
||||
}
|
||||
|
||||
try {
|
||||
long timestamp = Long.parseLong(parts[0]);
|
||||
return new Log(timestamp, parts[1], parts[2], parts[3]);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException("ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" + parts[0], e);
|
||||
}
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD><CDB3>
|
||||
public Map<String, Integer> levelmapCount() {
|
||||
HashMap<String, Integer> hmap = new HashMap<>();
|
||||
synchronized (LOCK) {
|
||||
for (Log log : logs) {
|
||||
String level = log.getLevel();
|
||||
hmap.put(level, hmap.getOrDefault(level, 0) + 1);
|
||||
}
|
||||
}
|
||||
return hmap;
|
||||
}
|
||||
|
||||
// <20><>ģ<EFBFBD><C4A3>ͳ<EFBFBD>ƴ<EFBFBD><C6B4><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DFBC><EFBFBD>key Ϊģ<CEAA><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
public Map<String, Integer> moduleErrorCount() {
|
||||
HashMap<String, Integer> hmap = new HashMap<>();
|
||||
synchronized (LOCK) {
|
||||
for (Log log : logs) {
|
||||
if ("ERROR".equals(log.getLevel())) {
|
||||
String module = log.getModule();
|
||||
hmap.put(module, hmap.getOrDefault(module, 0) + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return hmap;
|
||||
}
|
||||
|
||||
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD> 3 <20><><EFBFBD><EFBFBD>־
|
||||
public List<Log> earlistLogsCount() {
|
||||
List<Log> copy;
|
||||
synchronized (LOCK) {
|
||||
copy = new ArrayList<>(logs);
|
||||
}
|
||||
Collections.sort(copy, Comparator.comparingLong(Log::getTimetamp));
|
||||
if (copy.size() <= 3) {
|
||||
return copy;
|
||||
} else {
|
||||
return copy.subList(0, 3);
|
||||
}
|
||||
}
|
||||
|
||||
// <20>ϱ<EFBFBD>ͳ<EFBFBD>ƽ<EFBFBD><C6BD>
|
||||
private void sendToServer(Map<String, Integer> levelStats, Map<String, Integer> moduleErrorStats, List<Log> earliestLogs) {
|
||||
try (Socket socket = new Socket(serverAdd, port);
|
||||
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
|
||||
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {
|
||||
|
||||
oos.writeObject(levelStats);
|
||||
oos.writeObject(moduleErrorStats);
|
||||
oos.writeObject(earliestLogs);
|
||||
oos.flush();
|
||||
|
||||
String response = (String) ois.readObject();
|
||||
System.out.println("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>" + response);
|
||||
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
System.out.println(e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
48
day02/src/com/inmind/student/张洁/LogServer.java
Normal file
48
day02/src/com/inmind/student/张洁/LogServer.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package com.inmind.student.张洁;
|
||||
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class LogServer {
|
||||
public static final int PORT = 8080;
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
ServerSocket ss = new ServerSocket(PORT);
|
||||
while(true){
|
||||
try{
|
||||
Socket s = ss.accept();
|
||||
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
|
||||
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
Map<String,Integer> levelmap = (Map<String, Integer>) ois.readObject();
|
||||
Map<String,Integer> mouduleErrorMap = (Map<String, Integer>) ois.readObject();
|
||||
List<Log> earlistLogs = (List<Log>) ois.readObject();
|
||||
|
||||
System.out.println("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD>ƣ<EFBFBD>");
|
||||
for(String key : levelmap.keySet()){
|
||||
System.out.println(key+"<EFBFBD><EFBFBD>"+levelmap.get(key));
|
||||
}
|
||||
System.out.println("ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD>ƣ<EFBFBD>");
|
||||
for(String key : mouduleErrorMap.keySet()){
|
||||
System.out.println(key+":"+mouduleErrorMap.get(key));
|
||||
}
|
||||
System.out.println("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><EFBFBD>");
|
||||
for (Log earlistLog : earlistLogs) {
|
||||
System.out.println(earlistLog);
|
||||
}
|
||||
oos.writeObject("<EFBFBD>ϱ<EFBFBD><EFBFBD>ɹ<EFBFBD>");
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
17
day02/src/com/inmind/student/张洁/Main.java
Normal file
17
day02/src/com/inmind/student/张洁/Main.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.inmind.student.张洁;
|
||||
|
||||
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
|
||||
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
|
||||
// to see how IntelliJ IDEA suggests fixing it.
|
||||
System.out.printf("Hello and welcome!");
|
||||
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
|
||||
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
|
||||
System.out.println("i = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
41
day02/src/com/inmind/student/张洁/logback.xml
Normal file
41
day02/src/com/inmind/student/张洁/logback.xml
Normal file
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<!--
|
||||
CONSOLE :表示当前的日志信息是可以输出到控制台的。
|
||||
-->
|
||||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<!--输出流对象 默认 System.out 改为 System.err-->
|
||||
<target>System.out</target>
|
||||
<encoder>
|
||||
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度
|
||||
%msg:日志消息,%n是换行符-->
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %c [%thread] : %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- File是输出的方向通向文件的 -->
|
||||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
<charset>utf-8</charset>
|
||||
</encoder>
|
||||
<!--日志输出路径-->
|
||||
<file>D:/log/inmind-data.log</file>
|
||||
<!--指定日志文件拆分和压缩规则-->
|
||||
<rollingPolicy
|
||||
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||
<!--通过指定压缩文件名称,来确定分割文件方式-->
|
||||
<fileNamePattern>D:/log/inmind-data-%i-%d{yyyy-MM-dd}-.log.gz</fileNamePattern>
|
||||
<!--文件拆分大小-->
|
||||
<maxFileSize>1MB</maxFileSize>
|
||||
</rollingPolicy>
|
||||
</appender>
|
||||
|
||||
<!--
|
||||
1、控制日志的输出情况:如,开启日志,取消日志
|
||||
-->
|
||||
<root level="debug">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
<appender-ref ref="FILE" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user