Skip to content

Commit

Permalink
Update logger setup and add detailed logging
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Oct 3, 2024
1 parent 58d1c06 commit a94eba1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
minecraftVersion=1.21.1
fancyLibVersion=1.0.31
fancyAnalyticsVersion=0.1.1
fancyLoggerVersion=0.0.5
fancyLoggerVersion=0.0.6
plotsquaredVersion=7.2.0
chatcolorhandlerVersion=5.0.1
cloudCoreVersion=2.0.0-rc.2
Expand Down
35 changes: 30 additions & 5 deletions src/main/java/de/oliver/fancynpcs/FancyNpcs.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import de.oliver.fancyanalytics.api.events.Event;
import de.oliver.fancyanalytics.api.metrics.MetricSupplier;
import de.oliver.fancyanalytics.logger.ExtendedFancyLogger;
import de.oliver.fancyanalytics.logger.LogLevel;
import de.oliver.fancyanalytics.logger.appender.Appender;
import de.oliver.fancyanalytics.logger.appender.ConsoleAppender;
import de.oliver.fancyanalytics.logger.appender.JsonAppender;
import de.oliver.fancylib.FancyLib;
import de.oliver.fancylib.Metrics;
import de.oliver.fancylib.VersionConfig;
Expand Down Expand Up @@ -45,10 +49,9 @@
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand All @@ -61,7 +64,7 @@ public class FancyNpcs extends JavaPlugin implements FancyNpcsPlugin {
public static final FeatureFlag PLAYER_NPCS_FEATURE_FLAG = new FeatureFlag("player-npcs", "Every player can only manage the npcs they have created", false);

private static FancyNpcs instance;
private final ExtendedFancyLogger fancyLogger = new ExtendedFancyLogger("FancyNpcs");
private final ExtendedFancyLogger fancyLogger;
private final ScheduledExecutorService npcThread;
private final FancyScheduler scheduler;
private final FancyNpcsConfigImpl config;
Expand All @@ -82,6 +85,21 @@ public class FancyNpcs extends JavaPlugin implements FancyNpcsPlugin {

public FancyNpcs() {
instance = this;

Appender consoleAppender = new ConsoleAppender("[{loggerName}] ({threadName}) {logLevel}: {message}");
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
File logsFile = new File("plugins/FancyNpcs/logs/FN-logs-" + date + ".json");
if (!logsFile.exists()) {
try {
logsFile.getParentFile().mkdirs();
logsFile.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
}
JsonAppender jsonAppender = new JsonAppender(false, false, true, logsFile.getPath());
this.fancyLogger = new ExtendedFancyLogger("FancyNpcs", LogLevel.INFO, List.of(consoleAppender, jsonAppender), new ArrayList<>());

this.npcThread = Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder()
.setNameFormat("FancyNpcs-Npcs")
Expand Down Expand Up @@ -132,6 +150,7 @@ public void onLoad() {
.withProperty("pluginVersion", getPluginMeta().getVersion())
);

fancyLogger.error("Unsupported minecraft server version.");
getLogger().warning("--------------------------------------------------");
getLogger().warning("Unsupported minecraft server version.");
getLogger().warning("This plugin only supports 1.19.4 - 1.21.1");
Expand Down Expand Up @@ -186,6 +205,7 @@ public void onEnable() {
return; // could not get the newest version or already on latest
}

fancyLogger.warn("You are not using the latest version of the FancyNpcs plugin.");
getLogger().warning("""
-------------------------------------------------------
Expand All @@ -197,6 +217,7 @@ Please update to the newest version (%s).
});

if (!ServerSoftware.isPaper()) {
fancyLogger.warn("You are not using Paper as server software.");
getLogger().warning("--------------------------------------------------");
getLogger().warning("It is recommended to use Paper as server software.");
getLogger().warning("Because you are not using paper, the plugin");
Expand Down Expand Up @@ -351,6 +372,8 @@ Please update to the newest version (%s).
.registerArguments()
.registerExceptionHandlers()
.registerCommands();

fancyLogger.info("FancyNpcs (" + versionConfig.getVersion() + ") has been enabled.");
}

@Override
Expand All @@ -359,6 +382,8 @@ public void onDisable() {
if (npcManager != null) {
npcManager.saveNpcs(true);
}

fancyLogger.info("FancyNpcs has been disabled.");
}

public ExtendedFancyLogger getFancyLogger() {
Expand Down

0 comments on commit a94eba1

Please sign in to comment.