Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logger Modifications #1528

Merged
merged 2 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/dansplugins/factionsystem/MedievalFactions.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,15 @@ private void handlebStatsIntegration() {
}

private void handleDynmapIntegration() {
logger.debug("Handling dynmap integration...");
if (DynmapIntegrator.hasDynmap()) {
logger.debug("Found dynmap! Scheduling claims update and updating claims.");
persistentData.getDynmapIntegrator().scheduleClaimsUpdate(600); // Check once every 30 seconds for updates.
persistentData.getDynmapIntegrator().updateClaims();
}
else {
logger.debug("Dynmap not found! Claims update will not be scheduled.");
}
}

private void handlePlaceholdersIntegration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ private MarkerSet initializeMarkerSet(MarkerSet set, String markerLabel) {
}

private boolean isDynmapPresent() {
logger.debug("Is dynmap present? " + (dynmap != null));
return (dynmap != null);
}

Expand Down
31 changes: 31 additions & 0 deletions src/main/java/dansplugins/factionsystem/utils/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

import dansplugins.factionsystem.MedievalFactions;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.logging.Level;

/**
Expand All @@ -26,6 +32,7 @@ public Logger(MedievalFactions medievalFactions) {
public void debug(String message) {
if (medievalFactions.isDebugEnabled()) {
medievalFactions.getLogger().log(Level.INFO, "[Medieval Factions DEBUG] " + message);
logToFile("[DEBUG] " + message);
}
}

Expand All @@ -36,6 +43,7 @@ public void debug(String message) {
*/
public void print(String message) {
medievalFactions.getLogger().log(Level.INFO, "[Medieval Factions] " + message);
logToFile("[INFO] " + message);
}

/**
Expand All @@ -45,5 +53,28 @@ public void print(String message) {
*/
public void error(String message) {
medievalFactions.getLogger().log(Level.SEVERE, "[Medieval Factions ERROR] " + message);
logToFile("[ERROR] " + message);
}

private void logToFile(String message) {
// add time to message
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = LocalDateTime.now().format(formatter);
String dateMessage = "[" + formattedDateTime + "] " + message;

// append to file
File file = new File("plugins/MedievalFactions/logs.txt");
try {
if (!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(dateMessage);
bufferedWriter.newLine();
bufferedWriter.close();
} catch (IOException e) {
error("An error occurred while logging to file.");
}
}
}