feat: Rework how the Core-Logger works and is used

Implement own logger to prefix message with plugin name (if available) and CraftaroCore.

We prefix with both because the Core is shaded and this way it is clearly logged "which" Core is logging what.

This is also the reason why the Logger's name is now `CoreLogger.class.getCanonicalName()`
instead of CraftaroCore to uniquely identify each logger. (similar to what Bukkit's PluginLogger does).
This commit is contained in:
Christian Koop 2024-02-02 18:08:53 +01:00
parent e0dd19e91a
commit 92ff9cebd5
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
4 changed files with 54 additions and 18 deletions

View File

@ -0,0 +1,36 @@
package com.craftaro.core;
import org.bukkit.plugin.Plugin;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
public final class CoreLogger extends Logger {
private static final CoreLogger INSTANCE = new CoreLogger();
private final String corePrefix = "[" + CraftaroCoreConstants.getProjectName() + "] ";
private String pluginPrefix = "";
private CoreLogger() {
super(CoreLogger.class.getCanonicalName(), null);
setLevel(Level.ALL);
LogManager.getLogManager().addLogger(this);
}
protected void setPlugin(Plugin plugin) {
this.pluginPrefix = "[" + plugin.getName() + "] ";
}
@Override
public void log(LogRecord record) {
record.setMessage(this.pluginPrefix + this.corePrefix + record.getMessage());
super.log(record);
}
public static CoreLogger getInstance() {
return INSTANCE;
}
}

View File

@ -43,10 +43,9 @@ import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SongodaCore {
private static final Logger logger = Logger.getLogger(CraftaroCoreConstants.getProjectName());
private static final CoreLogger LOGGER = CoreLogger.getInstance();
/**
* Whenever we make a major change to the core GUI, updater,
@ -237,11 +236,11 @@ public class SongodaCore {
try {
verificationStatus = CraftaroProductVerification.getProductVerificationStatus(pluginID);
} catch (IOException ex) {
logger.log(Level.WARNING, "Error verifying plugin " + plugin.getName(), ex);
getLogger().log(Level.WARNING, "Error verifying plugin " + plugin.getName(), ex);
}
}
logger.info(getPrefix() + "Hooked " + plugin.getName() + ".");
getLogger().info(getPrefix() + "Hooked " + plugin.getName() + ".");
PluginInfo info = new PluginInfo(plugin, pluginID, icon, libraryVersion, verificationStatus);
// don't forget to check for language pack updates ;)
@ -289,9 +288,9 @@ public class SongodaCore {
}
} catch (IOException ex) {
final String er = ex.getMessage();
logger.log(Level.FINE, "Connection with Songoda servers failed: " + (er.contains("URL") ? er.substring(0, er.indexOf("URL") + 3) : er));
getLogger().log(Level.FINE, "Connection with Songoda servers failed: " + (er.contains("URL") ? er.substring(0, er.indexOf("URL") + 3) : er));
} catch (ParseException ex) {
logger.log(Level.FINE, "Failed to parse json for " + plugin.getJavaPlugin().getName() + " update check");
getLogger().log(Level.FINE, "Failed to parse json for " + plugin.getJavaPlugin().getName() + " update check");
}
}
@ -340,8 +339,8 @@ public class SongodaCore {
return "[" + CraftaroCoreConstants.getProjectName() + "] ";
}
public static Logger getLogger() {
return logger;
public static CoreLogger getLogger() {
return LOGGER;
}
public static boolean isRegistered(String plugin) {

View File

@ -96,6 +96,8 @@ public abstract class SongodaPlugin extends JavaPlugin {
@Override
public final void onLoad() {
SongodaCore.getLogger().setPlugin(this);
try {
//Load Core dependencies
Set<Dependency> dependencies = new HashSet<>(getDependencies());

View File

@ -1,5 +1,6 @@
package com.craftaro.core.dependency;
import com.craftaro.core.SongodaCore;
import com.georgev22.api.libraryloader.LibraryLoader;
import me.lucko.jarrelocator.JarRelocator;
import me.lucko.jarrelocator.Relocation;
@ -14,14 +15,12 @@ import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class DependencyLoader {
public static final String DEPENDENCY_VERSION = "v1";
private static final Logger logger = Logger.getLogger("CraftaroCore");
private static final LibraryLoader libraryLoader = new LibraryLoader(new File("plugins/CraftaroCore/dependencies/" + DEPENDENCY_VERSION));
private static final LibraryLoader libraryLoader = new LibraryLoader(DependencyLoader.class.getClassLoader(), new File("plugins/CraftaroCore/dependencies/" + DEPENDENCY_VERSION), SongodaCore.getLogger());
public static LibraryLoader getLibraryLoader() {
return libraryLoader;
@ -56,7 +55,7 @@ public class DependencyLoader {
}
try {
logger.info("[CraftaroCore] Downloading dependency " + groupId + ":" + artifactId + ":" + version + " from " + repositoryUrl);
SongodaCore.getLogger().info("Downloading dependency " + groupId + ":" + artifactId + ":" + version + " from " + repositoryUrl);
// Construct the URL for the artifact in the Maven repository
String artifactUrl = repositoryUrl + "/" +
groupId.replace('.', '/') + "/" +
@ -85,7 +84,7 @@ public class DependencyLoader {
out.close();
//Load dependency into the classpath
logger.info("[CraftaroCore] Downloaded dependency " + groupId + ":" + artifactId + ":" + version);
SongodaCore.getLogger().info("Downloaded dependency " + groupId + ":" + artifactId + ":" + version);
loadJarIntoClasspath(outputFile, dependency);
} catch (Exception e) {
e.printStackTrace();
@ -94,7 +93,7 @@ public class DependencyLoader {
public static void loadJarIntoClasspath(File file, Dependency dependency) {
if (!isRelocated(file) && dependency.shouldRelocate()) {
logger.info("[CraftaroCore] Loading dependency for relocation " + file);
SongodaCore.getLogger().info("Loading dependency for relocation " + file);
//relocate package to com.craftaro.core.third_party to avoid conflicts
List<Relocation> relocations = new ArrayList<>();
@ -107,13 +106,13 @@ public class DependencyLoader {
JarRelocator relocator = new JarRelocator(file, finalJar, relocations);
try {
relocator.run();
logger.info("[CraftaroCore] Relocated dependency " + file);
SongodaCore.getLogger().info("Relocated dependency " + file);
//Delete the old jar
file.delete();
} catch (Exception e) {
logger.severe("[CraftaroCore] Failed to relocate dependency1 " + file);
SongodaCore.getLogger().severe("Failed to relocate dependency1 " + file);
if (e.getMessage().contains("zip file is empty")) {
logger.severe("Try deleting the 'server root/craftaro' folder and restarting the server");
SongodaCore.getLogger().severe("Try deleting the 'server root/craftaro' folder and restarting the server");
}
e.printStackTrace();
//Delete the new jar cuz it's probably corrupted
@ -126,7 +125,7 @@ public class DependencyLoader {
} catch (Exception ignored) {
//already loaded
}
logger.info("[CraftaroCore] ----------------------------");
SongodaCore.getLogger().info("----------------------------");
}
private static boolean isRelocated(File file) {