Skip to content

Commit 04bbb66

Browse files
Made logger optional
1 parent 5608025 commit 04bbb66

4 files changed

Lines changed: 29 additions & 7 deletions

File tree

src/main/java/net/mackenziemolloy/shopguiplus/sellgui/SellGUI.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ public void onEnable() {
5454
generateFiles();
5555
setupMetrics();
5656
setupUpdates();
57-
58-
fileLogger = Logger.getLogger("SellGUIFileLogger");
5957
initLogger();
6058

6159
logger.info("*-*");
@@ -75,6 +73,9 @@ public void checkCompatibility() {
7573
}
7674

7775
public void initLogger() {
76+
if (this.configuration.get("options.transaction_log.enabled") == null || !this.configuration.getBoolean("options.transaction_log.enabled")) return;
77+
78+
fileLogger = Logger.getLogger("SellGUIFileLogger");
7879
File log = FileUtils.loadFile("transaction.log");
7980
FileHandler handler = null;
8081

@@ -91,6 +92,14 @@ public void initLogger() {
9192
fileLogger.setUseParentHandlers(false);
9293
}
9394

95+
public void closeLogger() {
96+
if (fileLogger == null) return;
97+
if (fileLogger.getHandlers().length == 0) return;
98+
99+
fileLogger.getHandlers()[0].close();
100+
fileLogger = null;
101+
}
102+
94103
public CommentedConfiguration getConfiguration() {
95104
return this.configuration;
96105
}

src/main/java/net/mackenziemolloy/shopguiplus/sellgui/command/CommandSellGUI.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ private boolean commandReload(CommandSender sender) {
133133
return;
134134
}
135135

136+
// This looks painful, I know.
137+
if (this.plugin.getConfiguration().get("options.transaction_log.enabled") != null && !this.plugin.getConfiguration().getBoolean("options.transaction_log.enabled")) this.plugin.closeLogger();
138+
else if(this.plugin.fileLogger == null) this.plugin.initLogger();
139+
136140
sendMessage(sender, "reloaded_config");
137141
if(sender instanceof Player) {
138142
Player player = (Player) sender;
@@ -600,7 +604,7 @@ private void onGuiClose(Player player, InventoryCloseEvent event, Set<Integer> i
600604
}
601605

602606
/* Subject to deprecation */
603-
plugin.fileLogger.info(player.getName() + " (" + player.getUniqueId() + ") sold: {" + HexColorUtility.purgeAllColor(receiptList.stream().collect(Collectors.joining(", "))) + "}");
607+
if (plugin.fileLogger != null) plugin.fileLogger.info(player.getName() + " (" + player.getUniqueId() + ") sold: {" + HexColorUtility.purgeAllColor(receiptList.stream().collect(Collectors.joining(", "))) + "}");
604608

605609

606610
if(configuration.getBoolean("options.sell_titles")) {

src/main/java/net/mackenziemolloy/shopguiplus/sellgui/utility/LogFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
public class LogFormatter extends Formatter {
1212

13-
private final DateFormat dateFormat = new SimpleDateFormat(SellGUI.getInstance().getConfiguration().getString("options.date_format"));
13+
private final DateFormat dateFormat = new SimpleDateFormat(SellGUI.getInstance().getConfiguration().getString("options.transaction_log.date_format"));
1414

1515
@Override
1616
public String format(LogRecord logRecord) {

src/main/resources/config.yml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
# |____/|_____|_____|_____\____|\___/|___|
77
# ( By Mackenzie Molloy )
88

9-
# Support Discord | https://mackenziemolloy.net/discord
9+
# Please contact me if you have any issues with the plugin - happy to help.
10+
# - Support Discord | https://mackenziemolloy.net/discord
1011

1112
#
1213
# Permissions:
@@ -23,8 +24,16 @@ options:
2324
receipt_type: 1
2425
sell_titles: true
2526

26-
# Date format of transactions logging (http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)
27-
date_format: "yyyy/MM/dd HH:mm:ss"
27+
#
28+
# This is the feature where all sell transactions made via
29+
# the SellGUI Sell Menu are logged to a file.
30+
#
31+
32+
transaction_log:
33+
# Should the Transaction logging feature be enabled?
34+
enabled: true
35+
# Date format of transactions logging (http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)
36+
date_format: "yyyy/MM/dd HH:mm:ss"
2837

2938
#
3039
# Whether prices should be rounded to 2 decimal places, or not.

0 commit comments

Comments
 (0)