package xitongshezhi;
|
|
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)) {
|
props.load(fis);
|
} 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)) {
|
// 添加文件头注释
|
props.store(fos, "操作日志记录 - 最后更新: " + 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("=== 日志结束 ===");
|
}
|
|
}
|