improve info/error logging (use plugin logger)

This commit is contained in:
Ivy Collective 2025-01-11 23:35:20 -05:00
parent 3d80f232b1
commit f21e9eb655

View file

@ -45,7 +45,7 @@ public class CloudflareTunnelsPlugin extends JavaPlugin implements Listener {
try {
return Runtime.getRuntime().exec(cmdline);
} catch (IOException e) {
e.printStackTrace();
logError("Failed to execute command: " + String.join(" ", cmdline), e);
return null;
}
}
@ -88,7 +88,7 @@ public class CloudflareTunnelsPlugin extends JavaPlugin implements Listener {
* Download a file from a url
*/
private boolean downloadFile(String urlString, File where) {
System.out.println("Downloading cloudflared, please wait (from " + urlString + ")");
getLogger().info("Downloading cloudflared, please wait (from " + urlString + ")");
URL url;
try {
url = URI.create(urlString).toURL();
@ -100,10 +100,10 @@ public class CloudflareTunnelsPlugin extends JavaPlugin implements Listener {
FileOutputStream outputStream = new FileOutputStream(where);
) {
outputStream.getChannel().transferFrom(bytes, 0, Long.MAX_VALUE);
System.out.println("Downloaded cloudflared");
getLogger().info("Downloaded cloudflared");
return true;
} catch (IOException e) {
e.printStackTrace();
logError(null, e);
return false;
}
}
@ -129,34 +129,39 @@ public class CloudflareTunnelsPlugin extends JavaPlugin implements Listener {
*/
@Override
public void onEnable() {
if (isBrokenState || shouldRun) {
throw new RuntimeException("Please restart the server, broken state detected.");
}
saveDefaultConfig();
if (!getConfig().getBoolean("installed")) {
throw new RuntimeException("Please configure this plugin and set 'installed: true' before starting the server");
}
Configuration config = new Configuration(
getConfig().getString("download.url"),
getConfig().getString("download.version"),
getConfig().getString("token"),
getConfig().getBoolean("restart.auto-restart"),
getConfig().getInt("restart.interval-seconds"),
getConfig().getBoolean("notify-ops-on-exit"),
getConfig().getString("log-level"),
getConfig().getBoolean("disable-tls")
);
try {
if (isBrokenState || shouldRun) {
throw new RuntimeException("Please restart (not reload!) the server, broken state detected.");
}
saveDefaultConfig();
if (!getConfig().getBoolean("installed")) {
throw new RuntimeException("Please configure this plugin and set 'installed: true' before starting the server");
}
Configuration config = new Configuration(
getConfig().getString("download.url"),
getConfig().getString("download.version"),
getConfig().getString("token"),
getConfig().getBoolean("restart.auto-restart"),
getConfig().getInt("restart.interval-seconds"),
getConfig().getBoolean("notify-ops-on-exit"),
getConfig().getString("log-level"),
getConfig().getBoolean("disable-tls")
);
shouldRun = true;
cloudflaredLoop(config);
shouldRun = true;
cloudflaredLoop(config);
} catch (Exception e) {
logError("An exception occured in onEnable. Stopping plugin.", e);
setEnabled(false);
}
}
private void cloudflaredLoop(Configuration config) {
System.out.println("Spawning cloudflared...");
getLogger().info("Spawning cloudflared...");
final Process process = spawnCloudflared(config);
process.onExit().thenRun(() -> {
System.out.println("Cloudflared exiting with code " + process.exitValue());
getLogger().info("Cloudflared exiting with code " + process.exitValue());
if (!shouldRun) {
return;
}
@ -165,14 +170,14 @@ public class CloudflareTunnelsPlugin extends JavaPlugin implements Listener {
Bukkit.broadcast(Component.text("Cloudflared died, restarting..."), Server.BROADCAST_CHANNEL_ADMINISTRATIVE);
}
if (config.restartInterval > 0) {
System.out.println("Restarting automatically in " + config.restartInterval + " seconds");
getLogger().info("Restarting automatically in " + config.restartInterval + " seconds");
Bukkit.getScheduler().runTaskLater(this, () -> cloudflaredLoop(config), config.restartInterval * 20L);
} else {
System.out.println("Restarting automatically now");
getLogger().info("Restarting automatically now");
cloudflaredLoop(config);
}
} else {
System.out.println("Not restarting automatically (feature disabled)");
getLogger().info("Not restarting automatically (feature disabled)");
if (config.notifyOps) {
Bukkit.broadcast(Component.text("Cloudflared died. Not restarting."), Server.BROADCAST_CHANNEL_ADMINISTRATIVE);
}
@ -185,21 +190,48 @@ public class CloudflareTunnelsPlugin extends JavaPlugin implements Listener {
*/
@Override
public void onDisable() {
shouldRun = false;
Process process = exec(new String[] { "pkill", "cloudflared" });
int result;
try {
result = process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
result = 1;
if (!shouldRun) {
getLogger().warning("shouldRun is false, assuming cloudflared is not running. Check for errors above!");
return;
}
shouldRun = false;
Process process = exec(new String[] { "pkill", "cloudflared" });
int result;
try {
result = process.waitFor();
} catch (InterruptedException e) {
logError(null, e);
result = 1;
}
if (result != 0) {
isBrokenState = true;
throw new RuntimeException("Failed to stop cloudflared");
} else {
getLogger().info("Stopped cloudflared");
}
} catch (Exception e) {
logError("An exception occured in onDisable. Cloudflared may not have stopped properly.", e);
}
if (result != 0) {
isBrokenState = true;
throw new RuntimeException("Failed to stop cloudflared");
} else {
System.out.println("Stopped cloudflared");
}
private void logError(String message, Exception e) {
String authors = String.join(", ", getPluginMeta().getAuthors());
String repo = getPluginMeta().getWebsite();
var logger = getLogger();
logger.severe("--------------------------------------");
logger.severe("An error has occured in " + getName() + ":");
if (message != null) logger.severe(message);
if (message != null) logger.severe("--- More details:");
logger.severe(e.getClass().getName() + ": " + e.getMessage());
logger.severe("");
for (StackTraceElement element : e.getStackTrace()) {
logger.severe("\t at " + element.toString());
}
logger.severe("");
logger.severe("If this is a bug, please report it to author(s) " + authors + " at " + repo);
logger.severe("--------------------------------------");
}
private record Configuration(