| | |
| | | import java.io.File; |
| | | import java.io.FileWriter; |
| | | import java.io.IOException; |
| | | import java.util.Date; // 修复:使用 java.util.Date 而不是 java.sql.Date |
| | | import java.util.Date; // 修复:使用 java.util.Date 而不是 java.sql.Date |
| | | import java.text.SimpleDateFormat; |
| | | |
| | | /** |
| | | * 日志记录工具类 |
| | | * 日志记录工具类 |
| | | */ |
| | | public class LogUtil { |
| | | private static final String LOG_DIR = "systemfile/logfile"; |
| | |
| | | private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | /** |
| | | * 记录程序启动日志 |
| | | * 记录程序启动日志 |
| | | */ |
| | | public static void logOpen() { |
| | | ensureLogDirExists(); |
| | | String timestamp = DATE_FORMAT.format(new Date()); |
| | | writeLog("程序启动: " + timestamp + "\n"); |
| | | writeLog("程序启动: " + timestamp + "\n"); |
| | | } |
| | | |
| | | /** |
| | | * 记录程序关闭日志并计算工作时长 |
| | | * @param startTime 程序启动时间戳 |
| | | * 记录程序关闭日志并计算工作时长 |
| | | * @param startTime 程序启动时间戳 |
| | | */ |
| | | public static void logClose(long startTime) { |
| | | ensureLogDirExists(); |
| | | long endTime = System.currentTimeMillis(); |
| | | String closeTime = DATE_FORMAT.format(new Date(endTime)); |
| | | |
| | | // 计算工作时长 |
| | | // 计算工作时长 |
| | | long duration = endTime - startTime; |
| | | String durationStr = formatDuration(duration); |
| | | |
| | | String log = "程序关闭: " + closeTime + "\n" + |
| | | "工作时长: " + durationStr + "\n" + |
| | | String log = "程序关闭: " + closeTime + "\n" + |
| | | "工作时长: " + durationStr + "\n" + |
| | | "-----------------------------------\n"; |
| | | writeLog(log); |
| | | } |
| | | |
| | | /** |
| | | * 确保日志目录存在 |
| | | * 确保日志目录存在 |
| | | */ |
| | | private static void ensureLogDirExists() { |
| | | File dir = new File(LOG_DIR); |
| | | if (!dir.exists()) { |
| | | boolean created = dir.mkdirs(); // 创建多级目录 |
| | | boolean created = dir.mkdirs(); // 创建多级目录 |
| | | if (!created) { |
| | | System.err.println("无法创建日志目录: " + LOG_DIR); |
| | | System.err.println("无法创建日志目录: " + LOG_DIR); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 格式化时长(毫秒→可读字符串) |
| | | * 格式化时长(毫秒→可读字符串) |
| | | */ |
| | | private static String formatDuration(long millis) { |
| | | long seconds = millis / 1000; |
| | | long hours = seconds / 3600; |
| | | long minutes = (seconds % 3600) / 60; |
| | | seconds = seconds % 60; |
| | | return String.format("%d小时 %d分钟 %d秒", hours, minutes, seconds); |
| | | return String.format("%d小时 %d分钟 %d秒", hours, minutes, seconds); |
| | | } |
| | | |
| | | /** |
| | | * 写入日志到文件(追加模式) |
| | | * 写入日志到文件(追加模式) |
| | | */ |
| | | private static void writeLog(String content) { |
| | | try (FileWriter fw = new FileWriter(LOG_FILE, true); |
| | | BufferedWriter bw = new BufferedWriter(fw)) { |
| | | bw.write(content); |
| | | bw.flush(); // 确保内容立即写入 |
| | | bw.flush(); // 确保内容立即写入 |
| | | } catch (IOException e) { |
| | | System.err.println("写入日志失败: " + e.getMessage()); |
| | | System.err.println("写入日志失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | public static void log(String string) { |
| | | // TODO 自动生成的方法存根 |
| | | // TODO 自动生成的方法存根 |
| | | |
| | | } |
| | | } |