张世豪
5 小时以前 a6077217e25f5804027194a5c2848e773eda1abd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package dialog;
 
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
 
public class Charulog {
    private static final String LOG_FILE = "log.properties";
    private static final int MAX_RECORDS = 500;
    private static final ReentrantLock lock = new ReentrantLock();
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
    /**
     * 静态方法:记录操作日志
     * @param operation 操作内容
     */
    public static void logOperation(String operation) {
        lock.lock();
        try {
            // 添加时间戳
            String timestamp = dateFormat.format(new Date());
            String logEntry = "[" + timestamp + "] " + operation;
            
            // 读取现有日志
            Properties logProps = readLogFile();
            
            // 添加新记录
            addNewLogEntry(logProps, logEntry);
            
            // 如果超过最大记录数,删除最早的记录
            if (logProps.size() > MAX_RECORDS) {
                removeOldestEntries(logProps);
            }
            
            // 写回文件
            writeLogFile(logProps);
            
        } catch (Exception e) {
            System.err.println("记录日志时发生错误: " + e.getMessage());
        } finally {
            lock.unlock();
        }
    }
    
    /**
     * 读取日志文件
     */
    private static Properties readLogFile() {
        Properties props = new Properties();
        File file = new File(LOG_FILE);
        
        if (file.exists()) {
            try (FileInputStream fis = new FileInputStream(file);
                 InputStreamReader isr = new InputStreamReader(fis, "UTF-8")) {
                props.load(isr);
            } catch (IOException e) {
                System.err.println("读取日志文件失败: " + e.getMessage());
            }
        }
        
        return props;
    }
    
    /**
     * 添加新的日志条目
     */
    private static void addNewLogEntry(Properties props, String logEntry) {
        // 使用时间戳作为key,确保唯一性
        String key = "log_" + System.currentTimeMillis() + "_" + UUID.randomUUID().toString().substring(0, 8);
        props.setProperty(key, logEntry);
    }
    
    /**
     * 移除最早的记录
     */
    private static void removeOldestEntries(Properties props) {
        // 将属性转换为列表以便排序
        List<Map.Entry<Object, Object>> entries = new ArrayList<>(props.entrySet());
        
        // 按键排序(假设键包含时间信息)
        entries.sort((e1, e2) -> {
            String key1 = (String) e1.getKey();
            String key2 = (String) e2.getKey();
            return key1.compareTo(key2);
        });
        
        // 删除超出限制的最早记录
        while (entries.size() > MAX_RECORDS) {
            Map.Entry<Object, Object> oldest = entries.remove(0);
            props.remove(oldest.getKey());
        }
    }
    
    /**
     * 写入日志文件
     */
    private static void writeLogFile(Properties props) {
        try (FileOutputStream fos = new FileOutputStream(LOG_FILE);
             OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8")) {
            // 添加文件头注释
            props.store(osw, "操作日志记录 - 最后更新: " + new Date());
        } catch (IOException e) {
            System.err.println("写入日志文件失败: " + e.getMessage());
        }
    }
    /**
     * 获取当前日志记录数量(用于测试)
     */
    public static int getLogCount() {
        Properties props = readLogFile();
        return props.size();
    }
    
    /**
     * 清空日志文件(用于测试)
     */
    public static void clearLogs() {
        lock.lock();
        try {
            Properties props = new Properties();
            writeLogFile(props);
        } finally {
            lock.unlock();
        }
    }
    
    /**
     * 读取并打印所有日志(用于测试)
     */
    public static void printAllLogs() {
        Properties props = readLogFile();
        List<Map.Entry<Object, Object>> entries = new ArrayList<>(props.entrySet());
        
        // 按键排序
        entries.sort((e1, e2) -> {
            String key1 = (String) e1.getKey();
            String key2 = (String) e2.getKey();
            return key1.compareTo(key2);
        });
        
        //System.out.println("=== 操作日志记录 (共 " + entries.size() + " 条) ===");
        for (Map.Entry<Object, Object> entry : entries) {
            //System.out.println(entry.getValue());
        }
        //System.out.println("=== 日志结束 ===");
    }
    
}