diff --git a/pom.xml b/pom.xml index 082fd85..ebf9a5a 100644 --- a/pom.xml +++ b/pom.xml @@ -11,6 +11,7 @@ 11 11 + 2.14.1 @@ -36,14 +37,19 @@ - org.tinylog - tinylog-api - 2.0.1 + org.apache.logging.log4j + log4j-api + ${slf4j.version} - org.tinylog - tinylog-impl - 2.0.1 + org.apache.logging.log4j + log4j-core + ${slf4j.version} + + + org.apache.logging.log4j + log4j-slf4j-impl + ${slf4j.version} diff --git a/src/main/java/io/graversen/minecraft/rcon/MinecraftClient.java b/src/main/java/io/graversen/minecraft/rcon/MinecraftClient.java index ba8fe60..5704e63 100644 --- a/src/main/java/io/graversen/minecraft/rcon/MinecraftClient.java +++ b/src/main/java/io/graversen/minecraft/rcon/MinecraftClient.java @@ -1,6 +1,6 @@ package io.graversen.minecraft.rcon; -import org.tinylog.Logger; +import lombok.extern.slf4j.Slf4j; import java.io.IOException; import java.net.InetSocketAddress; @@ -11,6 +11,7 @@ import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; +@Slf4j public class MinecraftClient implements IMinecraftClient { private static final int RCON_AUTHENTICATION_FAILURE = -1; private static final int RCON_COMMAND = 2; @@ -29,13 +30,9 @@ private MinecraftClient(SocketChannel rconSocketChannel, String hostname, int po this.currentRequestCounter = new AtomicInteger(1); this.executorService = Executors.newSingleThreadExecutor(); this.isConnected = true; - Logger.info("Initialized with connection tuple '{}'", connectionTuple); + log.info("Initialized with connection tuple '{}'", connectionTuple); } -// public static MinecraftClient connect(String hostname, String password) { -// return MinecraftClient.connect(hostname, password, MinecraftClient.DEFAULT_PORT); -// } - public static MinecraftClient connect(String hostname, String password, int port) { MinecraftClient minecraftClient = null; @@ -46,7 +43,7 @@ public static MinecraftClient connect(String hostname, String password, int port final Future authenticateResponse = minecraftClient.authenticateClient(password); final RconResponse rconResponse = authenticateResponse.get(5000, TimeUnit.MILLISECONDS); - Logger.info("Connection success!"); + log.info("Connection success!"); return minecraftClient; } catch (IOException | InterruptedException | ExecutionException e) { if (minecraftClient != null) minecraftClient.safeClose(); @@ -54,7 +51,7 @@ public static MinecraftClient connect(String hostname, String password, int port e, "Connection to %s:%d failed: %s", hostname, port, e.getCause() != null ? e.getCause().getMessage() : e.getMessage() ); } catch (TimeoutException e) { - if (minecraftClient != null) minecraftClient.safeClose(); + minecraftClient.safeClose(); throw new RconConnectException(e, "Connection to %s:%d timed out", hostname, port); } } @@ -65,7 +62,7 @@ public boolean isConnected(Duration timeout) { sendRawSilently("ping").get(timeout.toSeconds(), TimeUnit.SECONDS); return true; } catch (InterruptedException | ExecutionException | TimeoutException e) { - Logger.error("Lost connection to {}", connectionTuple); + log.error("Lost connection to {}", connectionTuple); safeClose(); return false; } @@ -181,11 +178,11 @@ private void safeClose() { } private Future authenticateClient(String password) { - Logger.debug("Authenticating..."); + log.debug("Authenticating..."); return sendRaw(RCON_AUTHENTICATION, password, true); } private void printCommand(String rawCommand) { - Logger.debug("Sending command: {}", rawCommand); + log.debug("Sending command: {}", rawCommand); } } diff --git a/src/main/java/io/graversen/minecraft/rcon/service/ConnectTask.java b/src/main/java/io/graversen/minecraft/rcon/service/ConnectTask.java index 22aef57..0b0bf99 100644 --- a/src/main/java/io/graversen/minecraft/rcon/service/ConnectTask.java +++ b/src/main/java/io/graversen/minecraft/rcon/service/ConnectTask.java @@ -2,10 +2,11 @@ import io.graversen.minecraft.rcon.MinecraftClient; import io.graversen.minecraft.rcon.RconConnectException; -import org.tinylog.Logger; +import lombok.extern.slf4j.Slf4j; import java.util.concurrent.Callable; +@Slf4j class ConnectTask implements Callable { private final ConnectOptions connectOptions; private final RconDetails rconDetails; @@ -13,8 +14,7 @@ class ConnectTask implements Callable { ConnectTask(ConnectOptions connectOptions, RconDetails rconDetails) { this.connectOptions = connectOptions; this.rconDetails = rconDetails; - Logger.debug(connectOptions); - Logger.debug(rconDetails); + log.debug("{}", connectOptions); } @Override @@ -23,17 +23,17 @@ public MinecraftClient call() throws Exception { while (currentAttempt < connectOptions.getMaxRetries() && !Thread.currentThread().isInterrupted()) { currentAttempt++; - Logger.debug("Connection attempt {}", currentAttempt); + log.debug("Connection attempt {}", currentAttempt); try { return MinecraftClient.connect(rconDetails.getHostname(), rconDetails.getPassword(), rconDetails.getPort()); } catch (Exception e) { - Logger.debug("Connection attempt failed due to: {}", e.getMessage()); + log.error("Connection attempt failed", e); } finally { if (currentAttempt < connectOptions.getMaxRetries()) { sleep(); } else { - Logger.debug("Ran out of retries after {} total attempts", currentAttempt); + log.warn("Ran out of retries after {} total attempts", currentAttempt); } } } @@ -43,7 +43,7 @@ public MinecraftClient call() throws Exception { private void sleep() { try { - Logger.debug("Pausing for {} ms", connectOptions.getTimeBetweenRetries().toMillis()); + log.debug("Pausing for {} ms", connectOptions.getTimeBetweenRetries().toMillis()); Thread.sleep(connectOptions.getTimeBetweenRetries().toMillis()); } catch (InterruptedException e) { e.printStackTrace(); diff --git a/src/main/java/io/graversen/minecraft/rcon/service/MinecraftRconService.java b/src/main/java/io/graversen/minecraft/rcon/service/MinecraftRconService.java index 7c2227f..c974e3d 100644 --- a/src/main/java/io/graversen/minecraft/rcon/service/MinecraftRconService.java +++ b/src/main/java/io/graversen/minecraft/rcon/service/MinecraftRconService.java @@ -2,7 +2,7 @@ import io.graversen.minecraft.rcon.IMinecraftClient; import io.graversen.minecraft.rcon.MinecraftRcon; -import org.tinylog.Logger; +import lombok.extern.slf4j.Slf4j; import java.time.Duration; import java.util.Optional; @@ -11,6 +11,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +@Slf4j public class MinecraftRconService implements IMinecraftRconService { private final RconDetails rconDetails; private final ConnectOptions connectOptions; @@ -74,7 +75,7 @@ public Optional minecraftRcon() { private void safeClose(String reason) { try { - Logger.info("Closing with reason: {}", reason); + log.info("Closing with reason: {}", reason); isConnected = false; if (minecraftClient != null) { @@ -103,7 +104,7 @@ public boolean onTestConnection() { public void onPingResult(PingResult pingResult) { if (!pingResult.isSuccess() && shouldConnect) { if (isConnected) { - Logger.warn("Connection broken - resetting"); + log.warn("Connection broken - resetting"); isConnected = false; minecraftClient = null; minecraftRcon = null; diff --git a/src/main/resources/tinylog.properties b/src/main/resources/tinylog.properties deleted file mode 100644 index a83ecba..0000000 --- a/src/main/resources/tinylog.properties +++ /dev/null @@ -1 +0,0 @@ -writer.format = {date: HH:mm:ss.SSS} {level|min-size=5} {pipe} {class-name|min-size=8} {pipe} {message} \ No newline at end of file