Refactor logging by replacing custom Logger with SLF4J and adapt all usages

This commit is contained in:
2025-07-24 23:04:52 +02:00
parent c66b30ff90
commit ab162b5094
18 changed files with 103 additions and 187 deletions
@@ -1,8 +1,8 @@
package com.alttd.playerutils.config;
import com.alttd.playerutils.PlayerUtils;
import com.alttd.playerutils.util.Logger;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
@@ -19,19 +19,16 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@SuppressWarnings({"unused", "SameParameterValue"})
@Slf4j @SuppressWarnings({"unused", "SameParameterValue"})
abstract class AbstractConfig {
File file;
YamlConfiguration yaml;
private static Logger logger = null;
AbstractConfig(PlayerUtils playerUtils, String filename, Logger logger) {
AbstractConfig.logger = logger;
AbstractConfig(PlayerUtils playerUtils, String filename) {
init(new File(playerUtils.getDataFolder(), filename), filename);
}
AbstractConfig(File file, String filename, Logger logger) {
AbstractConfig.logger = logger;
AbstractConfig(File file, String filename) {
init(new File(file.getPath() + File.separator + filename), filename);
}
@@ -41,10 +38,9 @@ abstract class AbstractConfig {
try {
yaml.load(file);
} catch (IOException ignore) {
} catch (InvalidConfigurationException ex) {
if (logger != null)
logger.severe(String.format("Could not load %s, please correct your syntax errors", filename));
throw new RuntimeException(ex);
} catch (InvalidConfigurationException e) {
log.error("Could not load {}, please correct your syntax errors", filename, e);
throw new RuntimeException(e);
}
yaml.options().copyDefaults(true);
}
@@ -59,10 +55,8 @@ abstract class AbstractConfig {
method.invoke(instance);
} catch (InvocationTargetException ex) {
throw new RuntimeException(ex.getCause());
} catch (Exception ex) {
if (logger != null)
logger.severe("Error invoking %.", method.toString());
ex.printStackTrace();
} catch (Exception e) {
log.error("Error invoking {}.", method, e);
}
}
}
@@ -75,10 +69,8 @@ abstract class AbstractConfig {
private void save() {
try {
yaml.save(file);
} catch (IOException ex) {
if (logger != null)
logger.severe("Could not save %.", file.toString());
ex.printStackTrace();
} catch (IOException e) {
log.error("Could not save {}.", file.toString(), e);
}
}
@@ -150,4 +142,4 @@ abstract class AbstractConfig {
ConfigurationSection getConfigurationSection(String path) {
return yaml.getConfigurationSection(path);
}
}
}