package net.limework.automaticlogdeleter; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; import java.io.File; import java.util.logging.Level; public class AutomaticLogDeleter extends JavaPlugin { @Override public void onEnable() { new Thread(() -> { this.saveDefaultConfig(); deleteLogs(); }).start(); } @Override public void onDisable() { FileConfiguration config = getConfig(); if (config.getBoolean("also-delete-logs-on-shutdown")) { //we aren't using separate thread here, so it has time to finish before server shutdowns deleteLogs(); } } public void deleteLogs() { File path = new File("logs"); File[] files = path.listFiles(); if (files == null) { return; } FileConfiguration config = getConfig(); //maximum file age allowed long maxOldMillis = config.getLong("delete-logs-older-than-seconds") * 1000; String fileName; for (File file : files) { if (file.isFile()) { fileName = file.getName(); //if file is not a log file //using contains to make it more likely to catch different types of logging systems if (!fileName.contains(".log") && !fileName.contains("log.")) { continue; } //if file is older than configured time if ((System.currentTimeMillis() - file.lastModified()) < maxOldMillis) { continue; } if (!file.delete()) { getLogger().log(Level.WARNING, "[AutomaticLogDeleter] Failed to delete server log file! Are you sure the file permissions are correct?"); } else { getLogger().log(Level.INFO, "[AutomaticLogDeleter] Deleted log file " + fileName); } } } } }