forked from LogBlock/LogBlock
Dumping log before deleting
This commit is contained in:
@@ -1,16 +1,22 @@
|
|||||||
package de.diddiz.LogBlock;
|
package de.diddiz.LogBlock;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public class ClearLog implements Runnable
|
public class ClearLog implements Runnable
|
||||||
{
|
{
|
||||||
private Connection conn;
|
private Connection conn;
|
||||||
|
private File dumbFolder;
|
||||||
|
|
||||||
public ClearLog(Connection conn) {
|
public ClearLog(Connection conn, File dataFolder) {
|
||||||
this.conn = conn;
|
this.conn = conn;
|
||||||
|
dumbFolder = new File(dataFolder, "dumb");
|
||||||
|
dumbFolder.mkdirs();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -20,16 +26,36 @@ public class ClearLog implements Runnable
|
|||||||
Statement state = null;
|
Statement state = null;
|
||||||
try {
|
try {
|
||||||
state = conn.createStatement();
|
state = conn.createStatement();
|
||||||
|
String time = new SimpleDateFormat("yy-MM-dd-HH-mm-ss").format(System.currentTimeMillis() - LogBlock.config.keepLogDays*86400000);
|
||||||
|
ResultSet rs;
|
||||||
for (String table : LogBlock.config.tables.values()) {
|
for (String table : LogBlock.config.tables.values()) {
|
||||||
int deleted = state.executeUpdate("DELETE FROM `" + table + "` WHERE date < date_sub(now(), INTERVAL " + LogBlock.config.keepLogDays + " DAY)");
|
rs = state.executeQuery("SELECT count(*) FROM `" + table + "` WHERE date < now()");
|
||||||
if (deleted > 0)
|
rs.next();
|
||||||
|
int deleted = rs.getInt(1);
|
||||||
|
if (deleted > 0) {
|
||||||
|
if (LogBlock.config.dumpDroppedLog)
|
||||||
|
state.execute("SELECT * FROM `" + table + "` WHERE date < '" + time + "' INTO OUTFILE '" + dumbFolder.getAbsolutePath().replace("\\", "\\\\") + "\\\\" + table + "-" + time + ".csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'");
|
||||||
|
state.execute("DELETE FROM `" + table + "` WHERE date < '" + time + "'");
|
||||||
LogBlock.log.info("[LogBlock] Cleared out table " + table + ". Deleted " + deleted + " entries.");
|
LogBlock.log.info("[LogBlock] Cleared out table " + table + ". Deleted " + deleted + " entries.");
|
||||||
deleted = state.executeUpdate("DELETE `" + table + "-sign` FROM `" + table + "-sign` LEFT JOIN `" + table + "` ON (`" + table + "-sign`.`id` = `" + table + "`.`id`) WHERE `" + table + "`.`id` IS NULL;");
|
}
|
||||||
if (deleted > 0)
|
rs = state.executeQuery("SELECT COUNT(*) FROM `" + table + "-sign` LEFT JOIN `" + table + "` USING (id) WHERE `" + table + "`.id IS NULL");
|
||||||
|
rs.next();
|
||||||
|
deleted = rs.getInt(1);
|
||||||
|
if (deleted > 0) {
|
||||||
|
if (LogBlock.config.dumpDroppedLog)
|
||||||
|
state.execute("SELECT * FROM `" + table + "-sign` LEFT JOIN `" + table + "` USING (id) WHERE `" + table + "`.id IS NULL INTO OUTFILE '" + dumbFolder.getAbsolutePath().replace("\\", "\\\\") + "\\\\" + table + "-sign-" + time + ".csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'");
|
||||||
|
state.execute("DELETE `" + table + "-sign` FROM `" + table + "-sign` LEFT JOIN `" + table + "` USING (id) WHERE `" + table + "`.id IS NULL;");
|
||||||
LogBlock.log.info("[LogBlock] Cleared out table " + table + "-sign. Deleted " + deleted + " entries.");
|
LogBlock.log.info("[LogBlock] Cleared out table " + table + "-sign. Deleted " + deleted + " entries.");
|
||||||
deleted = state.executeUpdate("DELETE `" + table + "-chest` FROM `" + table + "-chest` LEFT JOIN `" + table + "` ON (`" + table + "-chest`.`id` = `" + table + "`.`id`) WHERE `" + table + "`.`id` IS NULL;");
|
}
|
||||||
if (deleted > 0)
|
rs = state.executeQuery("SELECT COUNT(*) FROM `" + table + "-chest` LEFT JOIN `" + table + "` USING (id) WHERE `" + table + "`.id IS NULL");
|
||||||
|
rs.next();
|
||||||
|
deleted = rs.getInt(1);
|
||||||
|
if (deleted > 0) {
|
||||||
|
if (LogBlock.config.dumpDroppedLog)
|
||||||
|
state.execute("SELECT * FROM `" + table + "-chest` LEFT JOIN `" + table + "` USING (id) WHERE `" + table + "`.id IS NULL INTO OUTFILE '" + dumbFolder.getAbsolutePath().replace("\\", "\\\\") + "\\\\" + table + "-chest-" + time + ".csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'");
|
||||||
|
state.execute("DELETE `" + table + "-chest` FROM `" + table + "-chest` LEFT JOIN `" + table + "` USING (id) WHERE `" + table + "`.id IS NULL;");
|
||||||
LogBlock.log.info("[LogBlock] Cleared out table " + table + "-chest. Deleted " + deleted + " entries.");
|
LogBlock.log.info("[LogBlock] Cleared out table " + table + "-chest. Deleted " + deleted + " entries.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
LogBlock.log.log(Level.SEVERE, "[LogBlock] SQL exception", ex);
|
LogBlock.log.log(Level.SEVERE, "[LogBlock] SQL exception", ex);
|
||||||
|
@@ -16,6 +16,7 @@ public class Config {
|
|||||||
String dbUsername;
|
String dbUsername;
|
||||||
String dbPassword;
|
String dbPassword;
|
||||||
int keepLogDays;
|
int keepLogDays;
|
||||||
|
boolean dumpDroppedLog;
|
||||||
int delay;
|
int delay;
|
||||||
int defaultDist;
|
int defaultDist;
|
||||||
int defaultTime;
|
int defaultTime;
|
||||||
@@ -58,6 +59,8 @@ public class Config {
|
|||||||
config.setProperty("password", "pass");
|
config.setProperty("password", "pass");
|
||||||
if (!keys.contains("keepLogDays"))
|
if (!keys.contains("keepLogDays"))
|
||||||
config.setProperty("keepLogDays", -1);
|
config.setProperty("keepLogDays", -1);
|
||||||
|
if (!keys.contains("dumpDroppedLog"))
|
||||||
|
config.setProperty("dumpDroppedLog", true);
|
||||||
if (!keys.contains("delay"))
|
if (!keys.contains("delay"))
|
||||||
config.setProperty("delay", 6);
|
config.setProperty("delay", 6);
|
||||||
if (!keys.contains("defaultDist"))
|
if (!keys.contains("defaultDist"))
|
||||||
@@ -99,6 +102,7 @@ public class Config {
|
|||||||
dbUsername = config.getString("username");
|
dbUsername = config.getString("username");
|
||||||
dbPassword = config.getString("password");
|
dbPassword = config.getString("password");
|
||||||
keepLogDays = config.getInt("keepLogDays", -1);
|
keepLogDays = config.getInt("keepLogDays", -1);
|
||||||
|
dumpDroppedLog = config.getBoolean("dumpDroppedLog", true);
|
||||||
delay = config.getInt("delay", 6);
|
delay = config.getInt("delay", 6);
|
||||||
defaultDist = config.getInt("defaultDist", 20);
|
defaultDist = config.getInt("defaultDist", 20);
|
||||||
defaultTime = LogBlock.parseTimeSpec(config.getString("defaultTime"));
|
defaultTime = LogBlock.parseTimeSpec(config.getString("defaultTime"));
|
||||||
|
@@ -86,7 +86,7 @@ public class LogBlock extends JavaPlugin
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (config.keepLogDays >= 0)
|
if (config.keepLogDays >= 0)
|
||||||
new Thread(new ClearLog(getConnection())).start();
|
new Thread(new ClearLog(getConnection(), getDataFolder())).start();
|
||||||
LBBlockListener lbBlockListener = new LBBlockListener();
|
LBBlockListener lbBlockListener = new LBBlockListener();
|
||||||
LBPlayerListener lbPlayerListener = new LBPlayerListener();
|
LBPlayerListener lbPlayerListener = new LBPlayerListener();
|
||||||
PluginManager pm = getServer().getPluginManager();
|
PluginManager pm = getServer().getPluginManager();
|
||||||
|
Reference in New Issue
Block a user