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 ItemDatabase itemDatabase;
private static Logger logger; private static Logger logger;
private static Logger shopLogger;
private FileHandler handler; private FileHandler handler;
private List<PluginCommand> commands = new ArrayList<>(); private List<PluginCommand> commands = new ArrayList<>();
@ -125,6 +126,8 @@ public class ChestShop extends JavaPlugin {
public ChestShop() { public ChestShop() {
dataFolder = getDataFolder(); dataFolder = getDataFolder();
logger = getLogger(); logger = getLogger();
shopLogger = Logger.getLogger("ChestShop Shops");
shopLogger.setParent(logger);
description = getDescription(); description = getDescription();
server = getServer(); server = getServer();
plugin = this; plugin = this;
@ -165,20 +168,6 @@ public class ChestShop extends JavaPlugin {
registerPluginMessagingChannels(); 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(); startStatistics();
startBuildNotificatier(); startBuildNotificatier();
startUpdater(); startUpdater();
@ -201,6 +190,22 @@ public class ChestShop extends JavaPlugin {
NameManager.load(); NameManager.load();
commands.forEach(c -> c.setPermissionMessage(Messages.ACCESS_DENIED.getTextWithPrefix(null))); 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() { private void turnOffDatabaseLogging() {
@ -559,6 +564,10 @@ public class ChestShop extends JavaPlugin {
return dataFolder; return dataFolder;
} }
public static Logger getShopLogger() {
return shopLogger;
}
public static Logger getBukkitLogger() { public static Logger getBukkitLogger() {
return logger; return logger;
} }

View File

@ -255,10 +255,10 @@ public class Properties {
@ConfigurationComment("If true, plugin will log transactions in its own file") @ConfigurationComment("If true, plugin will log transactions in its own file")
public static boolean LOG_TO_FILE = false; 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; 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; public static boolean LOG_ALL_SHOP_REMOVALS = true;
@PrecededBySpace @PrecededBySpace

View File

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

View File

@ -36,6 +36,6 @@ public class TransactionLogger implements Listener {
event.getOwnerAccount().getName(), event.getOwnerAccount().getName(),
LocationUtil.locationToString(event.getSign().getLocation())); 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) @EventHandler(priority = EventPriority.MONITOR)
public static void onShopRemoval(final ShopDestroyedEvent event) { 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; return;
} }
@ -36,7 +36,7 @@ public class ShopRemovalLogger implements Listener {
prices, prices,
location); location);
ChestShop.getBukkitLogger().info(message); ChestShop.getShopLogger().info(message);
}); });
} }
} }