refactor: Further improvements to debug messages

feat/data-edit-commands
William 1 year ago
parent a6bab88cee
commit ca000197e4
No known key found for this signature in database

@ -50,6 +50,7 @@ import java.io.File;
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;
@ -247,10 +248,16 @@ public interface HuskSync extends Task.Supplier, EventDispatcher {
*/
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
*

@ -29,7 +29,6 @@ import redis.clients.jedis.exceptions.JedisException;
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;
@ -193,8 +192,8 @@ public class RedisManager extends JedisPubSub {
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);
}
@ -211,9 +210,8 @@ public class RedisManager extends JedisPubSub {
} 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);
}
@ -225,15 +223,15 @@ public class RedisManager extends JedisPubSub {
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();
}
@ -268,8 +266,8 @@ public class RedisManager extends JedisPubSub {
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);
}
@ -287,12 +285,12 @@ public class RedisManager extends JedisPubSub {
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);
@ -300,7 +298,7 @@ public class RedisManager extends JedisPubSub {
// 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();
}
}
@ -311,18 +309,18 @@ public class RedisManager extends JedisPubSub {
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;
}
}

@ -27,8 +27,6 @@ import net.william278.husksync.util.Task;
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;
@ -116,8 +114,8 @@ public abstract class DataSyncer {
}
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;
}

Loading…
Cancel
Save