Skip to content

Commit

Permalink
refactor: Further improvements to debug messages
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Dec 21, 2023
1 parent a6bab88 commit ca00019
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
9 changes: 8 additions & 1 deletion common/src/main/java/net/william278/husksync/HuskSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;

Expand Down Expand Up @@ -247,10 +248,16 @@ default void initialize(@NotNull String name, @NotNull ThrowingConsumer<HuskSync
*/
default void debug(@NotNull String message, @NotNull Throwable... throwable) {
if (getSettings().doDebugLogging()) {
log(Level.INFO, String.format("[DEBUG] %s", message), throwable);
log(Level.INFO, getDebugString(message), throwable);
}
}

// Get the debug log message format
@NotNull
private String getDebugString(@NotNull String message) {
return String.format("[DEBUG] [%s] %s", new SimpleDateFormat("mm:ss.SSS").format(new Date()), message);
}

/**
* Get the {@link AudienceProvider} instance
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import redis.clients.jedis.util.Pool;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -193,8 +192,8 @@ public void setUserData(@NotNull User user, @NotNull DataSnapshot.Packed data) {
RedisKeyType.DATA_UPDATE.getTimeToLive(),
data.asBytes(plugin)
);
plugin.debug(String.format("[%s] [%s] Set %s key from Redis server", user.getUsername(),
SimpleDateFormat.getDateTimeInstance().format(new Date()), RedisKeyType.DATA_UPDATE));
plugin.debug(String.format("[%s] Set %s key from Redis server",
user.getUsername(), RedisKeyType.DATA_UPDATE));
} catch (Throwable e) {
plugin.log(Level.SEVERE, "An exception occurred setting user data to Redis", e);
}
Expand All @@ -211,9 +210,8 @@ public void setUserCheckedOut(@NotNull User user, boolean checkedOut) {
} else {
jedis.del(getKey(RedisKeyType.DATA_CHECKOUT, user.getUuid(), clusterId));
}
plugin.debug(String.format("[%s] [%s] %s %s key from Redis server", user.getUsername(),
SimpleDateFormat.getDateTimeInstance().format(new Date()), checkedOut ? "Set" : "Removed",
RedisKeyType.DATA_CHECKOUT));
plugin.debug(String.format("[%s] %s %s key from Redis server", user.getUsername(),
checkedOut ? "Set" : "Removed", RedisKeyType.DATA_CHECKOUT));
} catch (Throwable e) {
plugin.log(Level.SEVERE, "An exception occurred setting checkout to", e);
}
Expand All @@ -225,15 +223,15 @@ public Optional<String> getUserCheckedOut(@NotNull User user) {
final byte[] key = getKey(RedisKeyType.DATA_CHECKOUT, user.getUuid(), clusterId);
final byte[] readData = jedis.get(key);
if (readData != null) {
plugin.debug(String.format("[%s] [%s] Read %s key from Redis server", user.getUsername(),
SimpleDateFormat.getDateTimeInstance().format(new Date()), RedisKeyType.DATA_CHECKOUT));
plugin.debug(String.format("[%s] Read %s key from Redis server", user.getUsername(),
RedisKeyType.DATA_CHECKOUT));
return Optional.of(new String(readData, StandardCharsets.UTF_8));
}
} catch (Throwable e) {
plugin.log(Level.SEVERE, "An exception occurred getting a user's checkout key from Redis", e);
}
plugin.debug(String.format("[%s] [%s] Waiting for %s key from Redis server", user.getUsername(),
SimpleDateFormat.getDateTimeInstance().format(new Date()), RedisKeyType.DATA_CHECKOUT));
plugin.debug(String.format("[%s] Waiting for %s key from Redis server", user.getUsername(),
RedisKeyType.DATA_CHECKOUT));
return Optional.empty();
}

Expand Down Expand Up @@ -268,8 +266,8 @@ public void setUserServerSwitch(@NotNull User user) {
getKey(RedisKeyType.SERVER_SWITCH, user.getUuid(), clusterId),
RedisKeyType.SERVER_SWITCH.getTimeToLive(), new byte[0]
);
plugin.debug(String.format("[%s] [%s] Set %s key to Redis server", user.getUsername(),
SimpleDateFormat.getDateTimeInstance().format(new Date()), RedisKeyType.SERVER_SWITCH));
plugin.debug(String.format("[%s] Set %s key to Redis server",
user.getUsername(), RedisKeyType.SERVER_SWITCH));
} catch (Throwable e) {
plugin.log(Level.SEVERE, "An exception occurred setting a user's server switch key from Redis", e);
}
Expand All @@ -287,20 +285,20 @@ public Optional<DataSnapshot.Packed> getUserData(@NotNull User user) {
final byte[] key = getKey(RedisKeyType.DATA_UPDATE, user.getUuid(), clusterId);
final byte[] dataByteArray = jedis.get(key);
if (dataByteArray == null) {
plugin.debug(String.format("[%s] [%s] Waiting for %s key from Redis server", user.getUsername(),
SimpleDateFormat.getDateTimeInstance().format(new Date()), RedisKeyType.DATA_UPDATE));
plugin.debug(String.format("[%s] Waiting for %s key from Redis server",
user.getUsername(), RedisKeyType.DATA_UPDATE));
return Optional.empty();
}
plugin.debug(String.format("[%s] [%s] Read %s key from Redis server", user.getUsername(),
SimpleDateFormat.getDateTimeInstance().format(new Date()), RedisKeyType.DATA_UPDATE));
plugin.debug(String.format("[%s] Read %s key from Redis server",
user.getUsername(), RedisKeyType.DATA_UPDATE));

// Consume the key (delete from redis)
jedis.del(key);

// Use Snappy to decompress the json
return Optional.of(DataSnapshot.deserialize(plugin, dataByteArray));
} catch (Throwable e) {
plugin.log(Level.SEVERE, "An exception occurred getting a user's data from redis", e);
plugin.log(Level.SEVERE, "An exception occurred getting a user's data from Redis", e);
return Optional.empty();
}
}
Expand All @@ -311,18 +309,18 @@ public boolean getUserServerSwitch(@NotNull User user) {
final byte[] key = getKey(RedisKeyType.SERVER_SWITCH, user.getUuid(), clusterId);
final byte[] readData = jedis.get(key);
if (readData == null) {
plugin.debug(String.format("[%s] [%s] Waiting for %s key from Redis server", user.getUsername(),
SimpleDateFormat.getDateTimeInstance().format(new Date()), RedisKeyType.SERVER_SWITCH));
plugin.debug(String.format("[%s] Waiting for %s key from Redis server",
user.getUsername(), RedisKeyType.SERVER_SWITCH));
return false;
}
plugin.debug(String.format("[%s] [%s] Read %s key from Redis server", user.getUsername(),
SimpleDateFormat.getDateTimeInstance().format(new Date()), RedisKeyType.SERVER_SWITCH));
plugin.debug(String.format("[%s] Read %s key from Redis server",
user.getUsername(), RedisKeyType.SERVER_SWITCH));

// Consume the key (delete from redis)
jedis.del(key);
return true;
} catch (Throwable e) {
plugin.log(Level.SEVERE, "An exception occurred getting a user's server switch from redis", e);
plugin.log(Level.SEVERE, "An exception occurred getting a user's server switch from Redis", e);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
Expand Down Expand Up @@ -116,8 +114,8 @@ protected void listenForRedisData(@NotNull OnlineUser user, @NotNull Supplier<Bo
}
if (plugin.isDisabling() || timesRun.getAndIncrement() > maxListenAttempts) {
task.get().cancel();
plugin.debug(String.format("[%s] [%s] Redis timed out after %s attempts; setting from database",
user.getUsername(), SimpleDateFormat.getDateTimeInstance().format(new Date()), timesRun.get()));
plugin.debug(String.format("[%s] Redis timed out after %s attempts; setting from database",
user.getUsername(), timesRun.get()));
setUserFromDatabase(user);
return;
}
Expand Down

0 comments on commit ca00019

Please sign in to comment.