Improve logger handling

- Log transactions with custom logger `ChestShop Shops`
- Fix that non-shop log messages show in log file (Fixes #551)
- Fix shop removal logging being broken
- Allow reloading `LOG_TO_FILE` and `LOG_TO_CONSOLE` config options
- Fix the wording in the comments on `LOG_TO_CONSOLE` and `LOG_ALL_SHOP_REMOVALS`
This commit is contained in:
Phoenix616 2023-06-18 22:12:08 +01:00
parent 54cc1ce842
commit 3a1885e2f3
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0
5 changed files with 29 additions and 20 deletions

View File

@ -118,6 +118,7 @@ public class ChestShop extends JavaPlugin {
private static ItemDatabase itemDatabase;
private static Logger logger;
private static Logger shopLogger;
private FileHandler handler;
private List<PluginCommand> commands = new ArrayList<>();
@ -125,6 +126,8 @@ public class ChestShop extends JavaPlugin {
public ChestShop() {
dataFolder = getDataFolder();
logger = getLogger();
shopLogger = Logger.getLogger("ChestShop Shops");
shopLogger.setParent(logger);
description = getDescription();
server = getServer();
plugin = this;
@ -165,20 +168,6 @@ public class ChestShop extends JavaPlugin {
registerPluginMessagingChannels();
if (Properties.LOG_TO_FILE) {
File log = loadFile("ChestShop.log");
FileHandler handler = loadHandler(log.getAbsolutePath());
handler.setFormatter(new FileFormatter());
this.handler = handler;
logger.addHandler(handler);
}
if (!Properties.LOG_TO_CONSOLE) {
logger.setUseParentHandlers(false);
}
startStatistics();
startBuildNotificatier();
startUpdater();
@ -201,6 +190,22 @@ public class ChestShop extends JavaPlugin {
NameManager.load();
commands.forEach(c -> c.setPermissionMessage(Messages.ACCESS_DENIED.getTextWithPrefix(null)));
if (handler != null) {
shopLogger.removeHandler(handler);
}
if (Properties.LOG_TO_FILE) {
if (handler == null) {
File log = loadFile("ChestShop.log");
handler = loadHandler(log.getAbsolutePath());
handler.setFormatter(new FileFormatter());
}
shopLogger.addHandler(handler);
}
shopLogger.setUseParentHandlers(Properties.LOG_TO_CONSOLE);
}
private void turnOffDatabaseLogging() {
@ -559,6 +564,10 @@ public class ChestShop extends JavaPlugin {
return dataFolder;
}
public static Logger getShopLogger() {
return shopLogger;
}
public static Logger getBukkitLogger() {
return logger;
}

View File

@ -255,10 +255,10 @@ public class Properties {
@ConfigurationComment("If true, plugin will log transactions in its own file")
public static boolean LOG_TO_FILE = false;
@ConfigurationComment("Do you want ChestShop's messages to show up in console?")
@ConfigurationComment("Do you want ChestShop's transaction messages to show up in console?")
public static boolean LOG_TO_CONSOLE = true;
@ConfigurationComment("Should all shop removals be logged to the console?")
@ConfigurationComment("Should all shop removals be logged?")
public static boolean LOG_ALL_SHOP_REMOVALS = true;
@PrecededBySpace

View File

@ -32,7 +32,7 @@ public class ShopCreationLogger implements Listener {
prices,
location);
ChestShop.getBukkitLogger().info(message);
ChestShop.getShopLogger().info(message);
});
}
}

View File

@ -36,6 +36,6 @@ public class TransactionLogger implements Listener {
event.getOwnerAccount().getName(),
LocationUtil.locationToString(event.getSign().getLocation()));
ChestShop.getBukkitLogger().info(message);
ChestShop.getShopLogger().info(message);
}
}

View File

@ -17,7 +17,7 @@ public class ShopRemovalLogger implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public static void onShopRemoval(final ShopDestroyedEvent event) {
if (Properties.LOG_ALL_SHOP_REMOVALS || event.getDestroyer() != null) {
if (!Properties.LOG_ALL_SHOP_REMOVALS && event.getDestroyer() != null) {
return;
}
@ -36,7 +36,7 @@ public class ShopRemovalLogger implements Listener {
prices,
location);
ChestShop.getBukkitLogger().info(message);
ChestShop.getShopLogger().info(message);
});
}
}