Skip to content

Commit

Permalink
Simplify logging
Browse files Browse the repository at this point in the history
  • Loading branch information
monst committed Feb 11, 2023
1 parent d329cbf commit c5ed447
Show file tree
Hide file tree
Showing 28 changed files with 108 additions and 98 deletions.
37 changes: 24 additions & 13 deletions src/main/java/com/monst/bankingplugin/BankingPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.logging.Level;

public class BankingPlugin extends JavaPlugin {

Expand All @@ -70,7 +71,7 @@ public class BankingPlugin extends JavaPlugin {
private Update update;

/* Debug */
private Logger logger = Logger.NO_OP; // Default to no-op logger, will be replaced by a real logger if debug is enabled
private Logger debugger = Logger.NO_OP; // Default to no-op logger, will be replaced by a real logger if debug is enabled

/* Startup Message */
private final String[] STARTUP_MESSAGE = new String[] {
Expand All @@ -96,7 +97,7 @@ public void onEnable() {
economy = findEconomy();
worths = new Worths(this, findEssentials());
} catch (MissingDependencyException e) {
getLogger().severe(e.getMessage());
log(Level.SEVERE, e.getMessage());
getServer().getPluginManager().disablePlugin(this);
return;
}
Expand All @@ -123,7 +124,7 @@ public void onEnable() {
checkForUpdates().then(update -> {
if (update == null)
return;
getLogger().warning("Version " + update.getVersion() + " of BankingPlugin is available!");
log(Level.WARNING, "Version " + update.getVersion() + " of BankingPlugin is available!");
if (config().downloadUpdatesAutomatically.get())
update.download();
}).catchError(error -> getLogger().warning("Failed to check for updates!"));
Expand Down Expand Up @@ -192,8 +193,7 @@ private void checkServerVersion() {
String packageName = Bukkit.getServer().getClass().getPackage().getName();
String serverVersion = packageName.substring(packageName.lastIndexOf('.') + 1);
if (!testedVersions.contains(serverVersion)) {
debug("Server version not officially supported: %s!", serverVersion);
getLogger().warning("Server version not officially supported: " + serverVersion + "!");
log(Level.WARNING, "Server version not officially supported: " + serverVersion + "!");
getLogger().warning("Plugin may still work, but more errors are expected!");
}
}
Expand Down Expand Up @@ -352,20 +352,31 @@ private void enableMetrics() {
metrics.addCustomChart(new SimplePie("self_banking", config().allowSelfBanking::toString));
metrics.addCustomChart(new SimplePie("language_file", config().languageFile::toString));
}


public void log(Level level, String message) {
getLogger().log(level, message);
debug(message);
}

public void log(Level level, String message, Object... format) {
String formatted = String.format(message, format);
getLogger().log(level, formatted);
debug(formatted);
}

/**
* Prints a message to the <i>/plugins/BankingPlugin/debug.txt</i> file.
* @param message the message to be printed
*/
public void debug(String message) {
logger.log(message);
debugger.log(message);
}

/**
* Prints a message with special formatting to the debug file.
*/
public void debug(String message, Object... format) {
logger.log(String.format(message, format));
debugger.log(String.format(message, format));
}

/**
Expand All @@ -375,22 +386,22 @@ public void debug(String message, Object... format) {
* @param throwable the {@link Throwable} of which the stacktrace will be printed
*/
public void debug(Throwable throwable) {
logger.log(throwable);
debugger.log(throwable);
}

public void setDebugLogEnabled(boolean enabled) {
logger.close();
debugger.close();
if (enabled) {
Path debugFile = getDataFolder().toPath().resolve("debug.txt");
try {
PrintWriter debugWriter = new PrintWriter(Files.newOutputStream(debugFile), true);
logger = Logger.printingTo(debugWriter);
debugger = Logger.printingTo(debugWriter);
return;
} catch (IOException e) {
getLogger().severe("Failed to create debug writer.");
getLogger().severe("Failed to create debug file.");
}
}
logger = Logger.NO_OP;
debugger = Logger.NO_OP;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -56,8 +57,7 @@ private void register() {
pluginCommand.setTabCompleter(this);

} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
plugin.getLogger().severe("Failed to create command \"" + name + "\"!");
plugin.debug("Failed to create command \"%s\"!", name);
plugin.log(Level.SEVERE, "Failed to create command '" + name + "'!");
plugin.debug(e);
return;
}
Expand All @@ -70,8 +70,7 @@ private void register() {
CommandMap commandMap = (CommandMap) commandMapField.get(Bukkit.getPluginManager());
commandMap.register(plugin.getName(), pluginCommand);
} catch (NoSuchFieldException | IllegalAccessException | ClassCastException e) {
plugin.getLogger().severe("Failed to register command \"" + name + "\"!");
plugin.debug("Failed to register command \"%s\"!", name);
plugin.log(Level.SEVERE, "Failed to register command '" + name + "'!");
plugin.debug(e);
}
}
Expand Down Expand Up @@ -105,7 +104,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
plugin.debug("%s is executing command /%s %s %s",
sender.getName(), name, subCommand.getName(), String.join(" ", arguments));
if (arguments.length < subCommand.getMinimumArguments()) {
plugin.debug("Too few arguments");
plugin.debug("Too few arguments. Sending usage message to " + sender.getName());
sender.sendMessage(subCommand.getUsageMessage()
.with(Placeholder.COMMAND).as(name + " " + subCommand.getName())
.translate(plugin));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected Message getNoPermissionMessage() {
protected void execute(Player player, String[] args) throws EventCancelledException {
new AccountCloseCommandEvent(player, args).fire();

plugin.debug("%s can now click a chest to close an account", player.getName());
plugin.debug("%s can now click a chest to close an account.", player.getName());
player.sendMessage(Message.CLICK_ACCOUNT_CLOSE.translate(plugin));
ClickAction.setAccountClickAction(player, account -> close(player, account));
}
Expand All @@ -63,7 +63,7 @@ private void close(Player player, Account account) throws CommandExecutionExcept

boolean balanceRemaining = account.getBalance().signum() > 0;
if ((balanceRemaining || plugin.config().confirmOnRemove.get()) && ClickAction.mustConfirm(player)) {
plugin.debug("Account removal needs confirmation");
plugin.debug("Account removal needs confirmation.");
if (balanceRemaining)
player.sendMessage(Message.ACCOUNT_BALANCE_NOT_ZERO
.with(Placeholder.ACCOUNT_BALANCE).as(plugin.getEconomy().format(account.getBalance().doubleValue()))
Expand All @@ -77,7 +77,7 @@ private void close(Player player, Account account) throws CommandExecutionExcept
}
ClickAction.remove(player);

plugin.debug("%s is removing account #%d", player.getName(), account.getID());
plugin.debug("%s is removing account %s", player.getName(), account);

new AccountCloseEvent(player, account).fire();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,14 @@ private void configure(Player executor, Account account, AccountField field, int
executor.sendMessage(Message.ACCOUNT_SET_INTEREST_MULTIPLIER
.with(Placeholder.INTEREST_MULTIPLIER).as(account.getInterestMultiplier(multipliers))
.translate(plugin));
plugin.debug("%s has set the multiplier stage of account #%d to %d",
executor.getName(), account.getID(), account.getInterestMultiplierStage());
plugin.debug("%s has set the multiplier stage of account %s", executor.getName(), account);
break;

case REMAINING_OFFLINE_PAYOUTS:

account.setRemainingOfflinePayouts(value);

plugin.debug("%s has set the remaining offline payouts of account #%d to %d.",
executor.getName(), account.getID(), account.getRemainingOfflinePayouts());
plugin.debug("%s has set the remaining offline payouts of account %s.", executor.getName(), account);
executor.sendMessage(Message.ACCOUNT_SET_REMAINING_OFFLINE
.with(Placeholder.VALUE).as(account.getRemainingOfflinePayouts())
.translate(plugin));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ protected Message getUsageMessage() {
protected void execute(Player player, String[] args) throws CommandExecutionException, EventCancelledException {
new AccountInfoCommandEvent(player, args).fire();

plugin.debug("%s can now click an account to see the GUI", player.getName());
plugin.debug("%s can now click an account to see the GUI.", player.getName());
player.sendMessage(Message.CLICK_ACCOUNT_INFO.translate(plugin));
ClickAction.setAccountClickAction(player, account -> info(player, account));
}

private void info(Player player, Account account) throws EventCancelledException {
ClickAction.remove(player);
plugin.debug("%s is viewing the GUI of account #%d", player.getName(), account.getID());
plugin.debug("%s is viewing the GUI of account %s", player.getName(), account);
new AccountInfoEvent(player, account).fire();
new AccountGUI(plugin, player, account).open();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected void execute(Player player, String[] args) throws EventCancelledExcept
new AccountMigrateCommandEvent(player, args).fire();
player.sendMessage(Message.CLICK_ACCOUNT_MIGRATE.translate(plugin));
ClickAction.setAccountClickAction(player, account -> selectAccount(player, account));
plugin.debug("%s is migrating an account", player.getName());
plugin.debug("%s must click a chest to migrate.", player.getName());
}

private void selectAccount(Player player, Account accountToMove) throws CommandExecutionException {
Expand All @@ -59,7 +59,7 @@ private void selectAccount(Player player, Account accountToMove) throws CommandE
throw err(Message.NO_PERMISSION_ACCOUNT_MIGRATE_OTHER);
}

plugin.debug("%s wants to migrate account #%d", player.getName(), accountToMove.getID());
plugin.debug("%s wants to migrate account %s", player.getName(), accountToMove);
ClickAction.setBlockClickAction(player, chest -> selectNewChest(player, accountToMove, chest));
player.sendMessage(Message.CLICK_CHEST_MIGRATE.translate(plugin));
}
Expand Down Expand Up @@ -158,7 +158,7 @@ private void selectNewChest(Player player, Account accountToMove, Block targetCh
accountToMove.updateChestTitle();
plugin.getAccountService().update(accountToMove);
player.sendMessage(Message.ACCOUNT_MIGRATED.translate(plugin));
plugin.debug("Migrated account #%d", accountToMove.getID());
plugin.debug("Migrated account %s", accountToMove);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ private void rename(Player executor, Account account, String newName) throws Com
throw err(Message.NO_PERMISSION_ACCOUNT_RENAME_OTHER);

if (newName.isEmpty()) {
plugin.debug("%s has reset nickname of account #%d", executor.getName(), account.getID());
account.setCustomName(null);
plugin.debug("%s has reset nickname of account %s", executor.getName(), account);
newName = account.getName();
} else {
plugin.debug("%s has renamed account #%d to \"%s\"", executor.getName(), account.getID(), newName);
account.setCustomName(newName);
plugin.debug("%s has renamed account %s", executor.getName(), account);
}
executor.sendMessage(Message.ACCOUNT_RENAMED.with(Placeholder.NAME).as(newName).translate(plugin));
new AccountRenameEvent(executor, account, newName).fire();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected void execute(Player player, String[] args) throws CommandExecutionExce
}

private void transfer(Player player, Account account, OfflinePlayer newOwner) throws CommandExecutionException, EventCancelledException {
plugin.debug("%s is transferring ownership of account #%d to %s", player.getName(), account.getID(), newOwner.getName());
plugin.debug("%s is transferring ownership of account %s", player.getName(), account);

if (!account.isOwner(player) && Permissions.ACCOUNT_TRANSFER_OTHER.notOwnedBy(player)) {
ClickAction.remove(player);
Expand All @@ -76,7 +76,7 @@ private void transfer(Player player, Account account, OfflinePlayer newOwner) th
}

if (plugin.config().confirmOnTransfer.get() && ClickAction.mustConfirm(player)) {
plugin.debug("Account transfer needs confirmation");
plugin.debug("Account transfer needs confirmation.");
player.sendMessage(Message.ACCOUNT_ABOUT_TO_TRANSFER
.with(Placeholder.PLAYER).as(newOwner.getName())
.and(Placeholder.ACCOUNT_ID).as(account.getID())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ private void trust(Player executor, Account account, OfflinePlayer playerToTrust
throw err(Message.ALREADY_CO_OWNER.with(Placeholder.PLAYER).as(playerToTrust.getName()));

new AccountTrustEvent(executor, account, playerToTrust).fire();

plugin.debug("%s has trusted %s to account #%d", executor.getName(), playerToTrust.getName(), account.getID());
executor.sendMessage(Message.ADDED_CO_OWNER.with(Placeholder.PLAYER).as(playerToTrust.getName()).translate(plugin));

account.trustPlayer(playerToTrust);
executor.sendMessage(Message.ADDED_CO_OWNER.with(Placeholder.PLAYER).as(playerToTrust.getName()).translate(plugin));
plugin.debug("%s has trusted %s to account %s", executor.getName(), playerToTrust.getName(), account);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ private void untrust(Player executor, Account account, OfflinePlayer playerToUnt
throw err(Message.NOT_A_CO_OWNER.with(Placeholder.PLAYER).as(playerToUntrust.getName()));

new AccountUntrustEvent(executor, account, playerToUntrust).fire();

plugin.debug("%s has untrusted %s from account #%d", executor.getName(), playerToUntrust.getName(), account.getID());
executor.sendMessage(Message.REMOVED_CO_OWNER.with(Placeholder.PLAYER).as(playerToUntrust.getName()).translate(plugin));

account.untrustPlayer(playerToUntrust);
executor.sendMessage(Message.REMOVED_CO_OWNER.with(Placeholder.PLAYER).as(playerToUntrust.getName()).translate(plugin));
plugin.debug("%s has untrusted %s from account %s", executor.getName(), playerToUntrust.getName(), account);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected void execute(Player player, String[] args) throws CommandExecutionExce
if (bank == null)
throw err(Message.BANK_NOT_FOUND.with(Placeholder.INPUT).as(args[0]));
}
plugin.debug("%s is viewing GUI of bank #%d", player.getName(), bank.getID());
plugin.debug("%s is viewing GUI of bank %s", player.getName(), bank);
new BankGUI(plugin, player, bank).open();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected void execute(CommandSender sender, String[] args) throws CommandExecut
plugin.getSchedulerService().scheduleAll();
if (sender instanceof Player && plugin.isGriefPreventionIntegrated())
BankVisualization.revert((Player) sender);
plugin.debug("Bank #%d and %d accounts removed from the database.", bank.getID(), accountsRemoved);
plugin.debug("Bank %s and %d accounts removed from the database.", bank, accountsRemoved);
String message = Message.BANK_REMOVED
.with(Placeholder.BANK_NAME).as(bank.getColorizedName())
.and(Placeholder.NUMBER_OF_ACCOUNTS).as(accountsRemoved)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ protected void execute(CommandSender sender, String[] args) throws CommandExecut
.with(Placeholder.NAME).as(newName)
.and(Placeholder.PATTERN).as(plugin.config().nameRegex.get()));

plugin.debug("%s is changing the name of bank #%d to %s", sender.getName(), bank.getID(), newName);
sender.sendMessage(Message.NAME_CHANGED.with(Placeholder.BANK_NAME).as(newName).translate(plugin));
bank.setName(newName);
sender.sendMessage(Message.NAME_CHANGED.with(Placeholder.BANK_NAME).as(newName).translate(plugin));
plugin.debug("%s is changing the name of bank %s", sender.getName(), bank);
plugin.getBankService().update(bank);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ protected void execute(Player player, String[] args) throws CommandExecutionExce
plugin.getBankService().update(bank);
if (plugin.isGriefPreventionIntegrated())
new BankVisualization(plugin, bank).show(player);
plugin.debug("%s has resized bank #%d", player.getName(), bank.getID());
player.sendMessage(Message.BANK_RESIZED.with(Placeholder.BANK_SIZE).as(volume).translate(plugin));
plugin.debug("%s has resized bank %s", player.getName(), bank);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ protected void execute(CommandSender sender, String[] args) throws CommandExecut
throw err(Message.ALREADY_OWNER.with(Placeholder.PLAYER).as(playerToTrust.getName()));
throw err(Message.ALREADY_CO_OWNER.with(Placeholder.PLAYER).as(playerToTrust.getName()));
}

plugin.debug("%s has trusted %s to bank #%d", sender.getName(), playerToTrust.getName(), bank.getID());
sender.sendMessage(Message.ADDED_CO_OWNER.with(Placeholder.PLAYER).as(playerToTrust.getName()).translate(plugin));

bank.trustPlayer(playerToTrust);
sender.sendMessage(Message.ADDED_CO_OWNER.with(Placeholder.PLAYER).as(playerToTrust.getName()).translate(plugin));
plugin.debug("%s has trusted %s to bank %s", sender.getName(), playerToTrust.getName(), bank);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ protected void execute(CommandSender sender, String[] args) throws CommandExecut

if (!bank.isTrusted(playerToUntrust))
throw err(Message.NOT_A_CO_OWNER.with(Placeholder.PLAYER).as(playerToUntrust.getName()));

plugin.debug("%s has untrusted %s from bank #%d", sender.getName(), playerToUntrust.getName(), bank.getID());
sender.sendMessage(Message.REMOVED_CO_OWNER.with(Placeholder.PLAYER).as(playerToUntrust.getName()).translate(plugin));

bank.untrustPlayer(playerToUntrust);
sender.sendMessage(Message.REMOVED_CO_OWNER.with(Placeholder.PLAYER).as(playerToUntrust.getName()).translate(plugin));
plugin.debug("%s has untrusted %s from bank %s", sender.getName(), playerToUntrust.getName(), bank);
}

@Override
Expand Down
Loading

0 comments on commit c5ed447

Please sign in to comment.