From 100f4dcea20a32663a07e91525de111f7515eb79 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期五, 21 十一月 2025 17:25:53 +0800
Subject: [PATCH] 可发布版本202517.25
---
src/xitongshezhi/lishijilu.java | 306 ++++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 269 insertions(+), 37 deletions(-)
diff --git a/src/xitongshezhi/lishijilu.java b/src/xitongshezhi/lishijilu.java
index 77cb8ae..29c2daf 100644
--- a/src/xitongshezhi/lishijilu.java
+++ b/src/xitongshezhi/lishijilu.java
@@ -17,20 +17,27 @@
private static final Color DARK_LIGHT_COLOR = new Color(26, 43, 68);
private static final Color TEXT_COLOR = new Color(224, 224, 224);
private static final Color TEXT_LIGHT_COLOR = new Color(160, 200, 255);
+ private static final Color DELETE_COLOR = new Color(231, 76, 60); // 鍒犻櫎鎸夐挳棰滆壊
+ private static final Color ACTIVE_BUTTON_COLOR = new Color(41, 128, 185); // 婵�娲绘寜閽鑹�
private JPanel mainPanel;
- private JLabel titleLabel;
+ private JButton logButton; // 鏃ュ織璁板綍鎸夐挳
+ private JButton errorLogButton; // 閿欒鏃ュ織鎸夐挳
private JButton backButton;
+ private JButton deleteAllButton;
// 鏂囨湰鍩熺粍浠�
private JScrollPane textScrollPane;
private JTextArea contentTextArea;
+ // 褰撳墠鏄剧ず鐨勬棩蹇楃被鍨�
+ private String currentLogType = "log"; // "log" 鎴� "error"
+
public lishijilu(JFrame parent) {
super(parent, "", true);
initializeUI();
setupEventListeners();
- loadLogContent(); // 鍔犺浇鏃ュ織鍐呭
+ loadLogContent(); // 榛樿鍔犺浇鎿嶄綔鏃ュ織
}
private void initializeUI() {
@@ -71,12 +78,48 @@
headerPanel.setOpaque(false);
headerPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 12, 0));
- // 鏍囬
- titleLabel = new JLabel("鍘嗗彶璁板綍");
- titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 22));
- titleLabel.setForeground(TEXT_COLOR);
+ // 鍒涘缓鏃ュ織绫诲瀷閫夋嫨鎸夐挳闈㈡澘
+ JPanel logTypePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 8, 0));
+ logTypePanel.setOpaque(false);
- // 杩斿洖鎸夐挳 - 浣跨敤涓嶉�忔槑璁捐
+ // 鏃ュ織璁板綍鎸夐挳
+ logButton = new JButton("鏃ュ織璁板綍");
+ logButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
+ logButton.setBackground(ACTIVE_BUTTON_COLOR); // 榛樿婵�娲荤姸鎬�
+ logButton.setForeground(Color.WHITE);
+ logButton.setOpaque(true);
+ logButton.setFocusPainted(false);
+ logButton.setBorder(BorderFactory.createEmptyBorder(8, 16, 8, 16));
+ logButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
+
+ // 閿欒鏃ュ織鎸夐挳
+ errorLogButton = new JButton("閿欒鏃ュ織");
+ errorLogButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
+ errorLogButton.setBackground(PRIMARY_COLOR); // 榛樿闈炴縺娲荤姸鎬�
+ errorLogButton.setForeground(Color.WHITE);
+ errorLogButton.setOpaque(true);
+ errorLogButton.setFocusPainted(false);
+ errorLogButton.setBorder(BorderFactory.createEmptyBorder(8, 16, 8, 16));
+ errorLogButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
+
+ logTypePanel.add(logButton);
+ logTypePanel.add(errorLogButton);
+
+ // 鎿嶄綔鎸夐挳闈㈡澘
+ JPanel actionButtonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 8, 0));
+ actionButtonPanel.setOpaque(false);
+
+ // 鍒犻櫎鍏ㄩ儴璁板綍鎸夐挳
+ deleteAllButton = new JButton("鍒犻櫎鍏ㄩ儴");
+ deleteAllButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
+ deleteAllButton.setBackground(DELETE_COLOR);
+ deleteAllButton.setForeground(Color.WHITE);
+ deleteAllButton.setOpaque(true);
+ deleteAllButton.setFocusPainted(false);
+ deleteAllButton.setBorder(BorderFactory.createEmptyBorder(8, 16, 8, 16));
+ deleteAllButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
+
+ // 杩斿洖鎸夐挳
backButton = new JButton("鍏抽棴");
backButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
backButton.setBackground(PRIMARY_COLOR);
@@ -85,6 +128,60 @@
backButton.setFocusPainted(false);
backButton.setBorder(BorderFactory.createEmptyBorder(8, 16, 8, 16));
backButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
+
+ actionButtonPanel.add(deleteAllButton);
+ actionButtonPanel.add(backButton);
+
+ // 鎸夐挳鎮仠鏁堟灉
+ setupButtonHoverEffects();
+
+ headerPanel.add(logTypePanel, BorderLayout.WEST);
+ headerPanel.add(actionButtonPanel, BorderLayout.EAST);
+
+ return headerPanel;
+ }
+
+ private void setupButtonHoverEffects() {
+ // 鏃ュ織鎸夐挳鎮仠鏁堟灉
+ logButton.addMouseListener(new java.awt.event.MouseAdapter() {
+ public void mouseEntered(java.awt.event.MouseEvent evt) {
+ if (!currentLogType.equals("log")) {
+ logButton.setBackground(brighterColor(PRIMARY_COLOR));
+ }
+ }
+
+ public void mouseExited(java.awt.event.MouseEvent evt) {
+ if (!currentLogType.equals("log")) {
+ logButton.setBackground(PRIMARY_COLOR);
+ }
+ }
+ });
+
+ // 閿欒鏃ュ織鎸夐挳鎮仠鏁堟灉
+ errorLogButton.addMouseListener(new java.awt.event.MouseAdapter() {
+ public void mouseEntered(java.awt.event.MouseEvent evt) {
+ if (!currentLogType.equals("error")) {
+ errorLogButton.setBackground(brighterColor(PRIMARY_COLOR));
+ }
+ }
+
+ public void mouseExited(java.awt.event.MouseEvent evt) {
+ if (!currentLogType.equals("error")) {
+ errorLogButton.setBackground(PRIMARY_COLOR);
+ }
+ }
+ });
+
+ // 鍒犻櫎鎸夐挳鎮仠鏁堟灉
+ deleteAllButton.addMouseListener(new java.awt.event.MouseAdapter() {
+ public void mouseEntered(java.awt.event.MouseEvent evt) {
+ deleteAllButton.setBackground(brighterColor(DELETE_COLOR));
+ }
+
+ public void mouseExited(java.awt.event.MouseEvent evt) {
+ deleteAllButton.setBackground(DELETE_COLOR);
+ }
+ });
// 杩斿洖鎸夐挳鎮仠鏁堟灉
backButton.addMouseListener(new java.awt.event.MouseAdapter() {
@@ -96,19 +193,6 @@
backButton.setBackground(PRIMARY_COLOR);
}
});
-
- JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
- titlePanel.setOpaque(false);
- titlePanel.add(titleLabel);
-
- JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
- buttonPanel.setOpaque(false);
- buttonPanel.add(backButton);
-
- headerPanel.add(titlePanel, BorderLayout.WEST);
- headerPanel.add(buttonPanel, BorderLayout.EAST);
-
- return headerPanel;
}
private JPanel createContentPanel() {
@@ -177,37 +261,138 @@
backButton.addActionListener(e -> {
dispose();
});
+
+ // 鍒犻櫎鍏ㄩ儴璁板綍鎸夐挳
+ deleteAllButton.addActionListener(e -> {
+ deleteAllRecords();
+ });
+
+ // 鏃ュ織璁板綍鎸夐挳
+ logButton.addActionListener(e -> {
+ switchToLogType("log");
+ });
+
+ // 閿欒鏃ュ織鎸夐挳
+ errorLogButton.addActionListener(e -> {
+ switchToLogType("error");
+ });
+ }
+
+ // 鍒囨崲鏃ュ織绫诲瀷
+ private void switchToLogType(String logType) {
+ if (currentLogType.equals(logType)) {
+ return; // 宸茬粡鏄綋鍓嶇被鍨嬶紝鏃犻渶鍒囨崲
+ }
+
+ currentLogType = logType;
+
+ // 鏇存柊鎸夐挳鐘舵��
+ if (logType.equals("log")) {
+ logButton.setBackground(ACTIVE_BUTTON_COLOR);
+ errorLogButton.setBackground(PRIMARY_COLOR);
+ } else {
+ logButton.setBackground(PRIMARY_COLOR);
+ errorLogButton.setBackground(ACTIVE_BUTTON_COLOR);
+ }
+
+ // 鍔犺浇瀵瑰簲绫诲瀷鐨勬棩蹇楀唴瀹�
+ loadLogContent();
+ }
+
+ // 鍒犻櫎鍏ㄩ儴璁板綍鐨勬柟娉�
+ private void deleteAllRecords() {
+ // 纭瀵硅瘽妗�
+ String logTypeName = currentLogType.equals("log") ? "鎿嶄綔" : "閿欒";
+ int result = JOptionPane.showConfirmDialog(
+ this,
+ "纭畾瑕佸垹闄ゆ墍鏈�" + logTypeName + "璁板綍鍚楋紵姝ゆ搷浣滀笉鍙仮澶嶏紒",
+ "纭鍒犻櫎",
+ JOptionPane.YES_NO_OPTION,
+ JOptionPane.WARNING_MESSAGE
+ );
+
+ if (result == JOptionPane.YES_OPTION) {
+ try {
+ String fileName = currentLogType.equals("log") ? "log.properties" : "err.properties";
+ File logFile = new File(fileName);
+
+ if (logFile.exists()) {
+ // 鍒涘缓绌虹殑Properties瀵硅薄骞跺啓鍏ユ枃浠�
+ Properties emptyProps = new Properties();
+ try (FileOutputStream out = new FileOutputStream(logFile);
+ OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8")) {
+ emptyProps.store(writer, "鍙戝崱鏈�" + logTypeName + "璁板綍 - 鎵�鏈夎褰曞凡娓呯┖");
+ }
+
+ // 鏇存柊鏂囨湰鍩熸樉绀�
+ contentTextArea.setText("鎵�鏈�" + logTypeName + "璁板綍宸叉垚鍔熷垹闄ゃ�俓n\n鏃ュ織鏂囦欢宸叉竻绌恒��");
+
+ // 鏄剧ず鎴愬姛娑堟伅
+ JOptionPane.showMessageDialog(
+ this,
+ "鎵�鏈�" + logTypeName + "璁板綍宸叉垚鍔熷垹闄ゃ��",
+ "鍒犻櫎鎴愬姛",
+ JOptionPane.INFORMATION_MESSAGE
+ );
+ } else {
+ contentTextArea.setText(logTypeName + "鏃ュ織鏂囦欢涓嶅瓨鍦紝鏃犻渶鍒犻櫎銆�");
+ }
+
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ JOptionPane.showMessageDialog(
+ this,
+ "鍒犻櫎璁板綍鏃跺嚭閿�: " + ex.getMessage(),
+ "閿欒",
+ JOptionPane.ERROR_MESSAGE
+ );
+ }
+ }
}
// 鍔犺浇鏃ュ織鍐呭
private void loadLogContent() {
- File logFile = new File("log.properties");
+ String fileName = currentLogType.equals("log") ? "log.properties" : "err.properties";
+ String logTypeName = currentLogType.equals("log") ? "鎿嶄綔" : "閿欒";
+
+ File logFile = new File(fileName);
if (!logFile.exists()) {
- contentTextArea.setText("鏃ュ織鏂囦欢涓嶅瓨鍦ㄣ��");
+ contentTextArea.setText(logTypeName + "鏃ュ織鏂囦欢涓嶅瓨鍦ㄣ��");
return;
}
Properties logProps = new Properties();
- try (FileInputStream in = new FileInputStream(logFile)) {
- logProps.load(in);
+ try (FileInputStream in = new FileInputStream(logFile);
+ InputStreamReader reader = new InputStreamReader(in, "UTF-8")) {
+
+ logProps.load(reader);
// 妫�鏌ヨ褰曟暟閲忥紝濡傛灉瓒呰繃1000鏉″垯鍒犻櫎鏃ц褰�
if (logProps.size() > 1000) {
- trimLogProperties(logProps);
+ trimLogProperties(logProps, fileName);
}
// 鏋勫缓鏄剧ず鍐呭
StringBuilder content = new StringBuilder();
- content.append("鏃ュ織鏂囦欢鍐呭 (").append(logProps.size()).append(" 鏉¤褰�):\n\n");
+ content.append(logTypeName).append("鏃ュ織鍐呭 (").append(logProps.size()).append(" 鏉¤褰�):\n\n");
// 鎸夋椂闂存埑鎺掑簭鏄剧ず
logProps.stringPropertyNames().stream()
- .sorted((a, b) -> Long.compare(Long.parseLong(b), Long.parseLong(a)))
+ .sorted((a, b) -> {
+ try {
+ // 浠庨敭涓彁鍙栨椂闂存埑閮ㄥ垎杩涜姣旇緝
+ long timeA = extractTimestampFromKey(a);
+ long timeB = extractTimestampFromKey(b);
+ return Long.compare(timeB, timeA); // 闄嶅簭鎺掑垪锛屾渶鏂扮殑鍦ㄥ墠
+ } catch (Exception e) {
+ return b.compareTo(a); // 濡傛灉鎻愬彇澶辫触锛屼娇鐢ㄥ瓧绗︿覆姣旇緝
+ }
+ })
.forEach(key -> {
String value = logProps.getProperty(key);
- content.append("鏃堕棿鎴�: ").append(key).append("\n");
- content.append("鍐呭: ").append(value).append("\n");
+ content.append("鏃堕棿: ").append(extractTimeFromValue(value)).append("\n");
+ content.append("鍐呭: ").append(extractOperationFromValue(value)).append("\n");
content.append("----------------------------------------\n");
});
@@ -215,23 +400,71 @@
} catch (IOException e) {
e.printStackTrace();
- contentTextArea.setText("鍔犺浇鏃ュ織鏂囦欢鏃跺嚭閿�: " + e.getMessage());
- } catch (NumberFormatException e) {
- contentTextArea.setText("鏃ュ織鏂囦欢鏍煎紡閿欒銆�");
+ contentTextArea.setText("鍔犺浇" + logTypeName + "鏃ュ織鏂囦欢鏃跺嚭閿�: " + e.getMessage());
+ } catch (Exception e) {
+ e.printStackTrace();
+ contentTextArea.setText("澶勭悊" + logTypeName + "鏃ュ織鍐呭鏃跺嚭閿�: " + e.getMessage());
}
}
+ // 浠庨敭涓彁鍙栨椂闂存埑
+ private long extractTimestampFromKey(String key) {
+ try {
+ // 閿殑鏍煎紡: log_鏃堕棿鎴砡UUID 鎴� error_鏃堕棿鎴砡UUID
+ String[] parts = key.split("_");
+ if (parts.length >= 2) {
+ return Long.parseLong(parts[1]);
+ }
+ } catch (Exception e) {
+ // 濡傛灉瑙f瀽澶辫触锛岃繑鍥�0
+ }
+ return 0L;
+ }
+
+ // 浠庡�间腑鎻愬彇鏃堕棿閮ㄥ垎
+ private String extractTimeFromValue(String value) {
+ if (value == null) return "鏈煡鏃堕棿";
+ // 鍊肩殑鏍煎紡: [2025-11-21 14:44:39] 鍙栧崱鎿嶄綔锛氬崱妲�19琚鐞嗗憳鍙栧崱
+ int start = value.indexOf('[');
+ int end = value.indexOf(']');
+ if (start >= 0 && end > start) {
+ return value.substring(start + 1, end);
+ }
+ return value;
+ }
+
+ // 浠庡�间腑鎻愬彇鎿嶄綔閮ㄥ垎
+ private String extractOperationFromValue(String value) {
+ if (value == null) return "鏈煡鍐呭";
+ // 鍊肩殑鏍煎紡: [2025-11-21 14:44:39] 鍙栧崱鎿嶄綔锛氬崱妲�19琚鐞嗗憳鍙栧崱
+ int end = value.indexOf(']');
+ if (end >= 0 && end + 1 < value.length()) {
+ return value.substring(end + 1).trim();
+ }
+ return value;
+ }
+
// 淇壀鏃ュ織灞炴�э紝鍙繚鐣欐渶鏂扮殑1000鏉¤褰�
- private void trimLogProperties(Properties logProps) {
+ private void trimLogProperties(Properties logProps, String fileName) {
// 鎸夋椂闂存埑鎺掑簭锛屼繚鐣欐渶鏂扮殑1000鏉�
logProps.stringPropertyNames().stream()
- .sorted((a, b) -> Long.compare(Long.parseLong(b), Long.parseLong(a)))
+ .sorted((a, b) -> {
+ try {
+ long timeA = extractTimestampFromKey(a);
+ long timeB = extractTimestampFromKey(b);
+ return Long.compare(timeB, timeA);
+ } catch (Exception e) {
+ return b.compareTo(a);
+ }
+ })
.skip(1000)
.forEach(logProps::remove);
// 淇濆瓨淇壀鍚庣殑灞炴��
- try (FileOutputStream out = new FileOutputStream("log.properties")) {
- logProps.store(out, "UWB浜哄憳瀹氫綅鍗″彂鍗℃満鍘嗗彶璁板綍 - 鑷姩淇壀鑷�1000鏉¤褰�");
+ try (FileOutputStream out = new FileOutputStream(fileName);
+ OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8")) {
+ String logTypeName = fileName.equals("log.properties") ? "鎿嶄綔" : "閿欒";
+ logProps.store(writer, "鍙戝崱鏈�" + logTypeName + "璁板綍 - 鑷姩淇壀鑷�1000鏉¤褰�");
} catch (IOException e) {
e.printStackTrace();
}
@@ -252,5 +485,4 @@
dialog.setVisible(true);
});
}
-
}
\ No newline at end of file
--
Gitblit v1.9.3