Skip to content

Commit

Permalink
✨ Add slf4j based logging
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGraversen committed Apr 14, 2021
1 parent 504495d commit b521b3d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
18 changes: 12 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<slf4j.version>2.14.1</slf4j.version>
</properties>

<dependencies>
Expand All @@ -36,14 +37,19 @@
</dependency>

<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog-api</artifactId>
<version>2.0.1</version>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog-impl</artifactId>
<version>2.0.1</version>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
Expand Down
19 changes: 8 additions & 11 deletions src/main/java/io/graversen/minecraft/rcon/MinecraftClient.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -46,15 +43,15 @@ public static MinecraftClient connect(String hostname, String password, int port
final Future<RconResponse> 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();
throw new RconConnectException(
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);
}
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -181,11 +178,11 @@ private void safeClose() {
}

private Future<RconResponse> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

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<MinecraftClient> {
private final ConnectOptions connectOptions;
private final RconDetails rconDetails;

ConnectTask(ConnectOptions connectOptions, RconDetails rconDetails) {
this.connectOptions = connectOptions;
this.rconDetails = rconDetails;
Logger.debug(connectOptions);
Logger.debug(rconDetails);
log.debug("{}", connectOptions);
}

@Override
Expand All @@ -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);
}
}
}
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -74,7 +75,7 @@ public Optional<MinecraftRcon> minecraftRcon() {

private void safeClose(String reason) {
try {
Logger.info("Closing with reason: {}", reason);
log.info("Closing with reason: {}", reason);
isConnected = false;

if (minecraftClient != null) {
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/tinylog.properties

This file was deleted.

0 comments on commit b521b3d

Please sign in to comment.