new changes

This commit is contained in:
minster586
2025-07-23 19:20:40 -04:00
parent 9d28e67d29
commit 9ee28713e6
2 changed files with 49 additions and 46 deletions

View File

@@ -9,68 +9,64 @@ import java.util.zip.ZipOutputStream;
public class LogWriter { public class LogWriter {
private static final String LOG_FOLDER = "plugins/ServerDevMode/logs/"; private static final String BASE_FOLDER = "plugins/ServerDevMode/logs/";
private static final String ZIP_FOLDER = "plugins/ServerDevMode/zipped_logs/"; private static final String ZIP_FOLDER = "plugins/ServerDevMode/zipped_logs/";
private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH-mm"); private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
private BufferedWriter currentWriter;
public LogWriter() { public LogWriter() {
prepareDirectories(); createFolder(BASE_FOLDER);
rotateLogFile(); // Start fresh createFolder(ZIP_FOLDER);
} }
private void prepareDirectories() { public void logToCategory(String category, String message) {
new File(LOG_FOLDER).mkdirs(); String folderPath = BASE_FOLDER + category + "/";
new File(ZIP_FOLDER).mkdirs(); createFolder(folderPath);
}
public void rotateLogFile() { String fileName = "log_" + FORMAT.format(new Date()) + ".log";
try { File file = new File(folderPath, fileName);
if (currentWriter != null) currentWriter.close();
String timestamp = FORMAT.format(new Date());
File file = new File(LOG_FOLDER + "devlog_" + timestamp + ".log");
currentWriter = new BufferedWriter(new FileWriter(file));
log("Log file initialized at " + timestamp);
} catch (IOException e) {
e.printStackTrace();
}
}
public void log(String message) { try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))) {
try {
String entry = "[" + FORMAT.format(new Date()) + "] " + message; String entry = "[" + FORMAT.format(new Date()) + "] " + message;
currentWriter.write(entry); writer.write(entry);
currentWriter.newLine(); writer.newLine();
currentWriter.flush();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); System.err.println("Failed to write log to " + category + ": " + e.getMessage());
} }
} }
private void createFolder(String path) {
File folder = new File(path);
if (!folder.exists()) folder.mkdirs();
}
public void zipLogs() { public void zipLogs() {
String zipName = "logs_" + FORMAT.format(new Date()) + ".zip"; String zipName = "logs_" + FORMAT.format(new Date()) + ".zip";
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(ZIP_FOLDER + zipName))) { File zipFile = new File(ZIP_FOLDER + zipName);
Files.walk(Paths.get(LOG_FOLDER))
.filter(Files::isRegularFile)
.forEach(path -> {
try (InputStream is = Files.newInputStream(path)) {
ZipEntry entry = new ZipEntry(path.getFileName().toString());
zos.putNextEntry(entry);
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
zos.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
});
System.out.println("[DevMode] Zipped logs to " + zipName); try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
Path basePath = Paths.get(BASE_FOLDER);
Files.walk(basePath)
.filter(Files::isRegularFile)
.forEach(path -> {
try (InputStream is = Files.newInputStream(path)) {
String entryName = basePath.relativize(path).toString().replace("\\", "/");
zos.putNextEntry(new ZipEntry(entryName));
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
zos.closeEntry();
} catch (IOException e) {
System.err.println("Failed to zip file: " + path + "" + e.getMessage());
}
});
System.out.println("[ServerDevMode] Logs zipped to " + zipFile.getName());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); System.err.println("Failed to create zip file: " + e.getMessage());
} }
} }
} }

View File

@@ -0,0 +1,7 @@
timestamp-format: "h:mm a | MM-dd-yyyy"
log-extension: "csv"
log-categories:
- chat
- commands
- events
- errors