new changes
This commit is contained in:
@@ -9,54 +9,50 @@ import java.util.zip.ZipOutputStream;
|
||||
|
||||
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 SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH-mm");
|
||||
|
||||
private BufferedWriter currentWriter;
|
||||
private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
|
||||
|
||||
public LogWriter() {
|
||||
prepareDirectories();
|
||||
rotateLogFile(); // Start fresh
|
||||
createFolder(BASE_FOLDER);
|
||||
createFolder(ZIP_FOLDER);
|
||||
}
|
||||
|
||||
private void prepareDirectories() {
|
||||
new File(LOG_FOLDER).mkdirs();
|
||||
new File(ZIP_FOLDER).mkdirs();
|
||||
}
|
||||
public void logToCategory(String category, String message) {
|
||||
String folderPath = BASE_FOLDER + category + "/";
|
||||
createFolder(folderPath);
|
||||
|
||||
public void rotateLogFile() {
|
||||
try {
|
||||
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();
|
||||
}
|
||||
}
|
||||
String fileName = "log_" + FORMAT.format(new Date()) + ".log";
|
||||
File file = new File(folderPath, fileName);
|
||||
|
||||
public void log(String message) {
|
||||
try {
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))) {
|
||||
String entry = "[" + FORMAT.format(new Date()) + "] " + message;
|
||||
currentWriter.write(entry);
|
||||
currentWriter.newLine();
|
||||
currentWriter.flush();
|
||||
writer.write(entry);
|
||||
writer.newLine();
|
||||
} 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() {
|
||||
String zipName = "logs_" + FORMAT.format(new Date()) + ".zip";
|
||||
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(ZIP_FOLDER + zipName))) {
|
||||
Files.walk(Paths.get(LOG_FOLDER))
|
||||
File zipFile = new File(ZIP_FOLDER + 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)) {
|
||||
ZipEntry entry = new ZipEntry(path.getFileName().toString());
|
||||
zos.putNextEntry(entry);
|
||||
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) {
|
||||
@@ -64,13 +60,13 @@ public class LogWriter {
|
||||
}
|
||||
zos.closeEntry();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("Failed to zip file: " + path + " — " + e.getMessage());
|
||||
}
|
||||
});
|
||||
|
||||
System.out.println("[DevMode] Zipped logs to " + zipName);
|
||||
System.out.println("[ServerDevMode] Logs zipped to " + zipFile.getName());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("Failed to create zip file: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
7
src/main/resources/config.yml
Normal file
7
src/main/resources/config.yml
Normal file
@@ -0,0 +1,7 @@
|
||||
timestamp-format: "h:mm a | MM-dd-yyyy"
|
||||
log-extension: "csv"
|
||||
log-categories:
|
||||
- chat
|
||||
- commands
|
||||
- events
|
||||
- errors
|
Reference in New Issue
Block a user