Skip to content

Commit

Permalink
⚗️ Switch logging mechanism to Tinylog2
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGraversen committed Jan 21, 2020
1 parent 558a432 commit 76aecb3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@
<artifactId>commons-text</artifactId>
<version>1.8</version>
</dependency>

<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<groupId>org.tinylog</groupId>
<artifactId>tinylog-impl</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>

Expand Down
14 changes: 6 additions & 8 deletions src/main/java/io/graversen/minecraft/rcon/MinecraftClient.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.graversen.minecraft.rcon;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tinylog.Logger;

import java.io.IOException;
import java.net.InetSocketAddress;
Expand All @@ -13,7 +12,6 @@
import java.util.concurrent.atomic.AtomicInteger;

public class MinecraftClient implements IMinecraftClient {
private static Logger LOG = LoggerFactory.getLogger(MinecraftClient.class);
private static final int RCON_AUTHENTICATION_FAILURE = -1;
private static final int RCON_COMMAND = 2;
private static final int RCON_AUTHENTICATION = 3;
Expand All @@ -31,7 +29,7 @@ private MinecraftClient(SocketChannel rconSocketChannel, String hostname, int po
this.currentRequestCounter = new AtomicInteger(1);
this.executorService = Executors.newSingleThreadExecutor();
this.isConnected = true;
LOG.info("Initialized with connection tuple '{}'", connectionTuple);
Logger.info("Initialized with connection tuple '{}'", connectionTuple);
}

// public static MinecraftClient connect(String hostname, String password) {
Expand All @@ -48,7 +46,7 @@ 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);

LOG.info("Connection success!");
Logger.info("Connection success!");
return minecraftClient;
} catch (IOException | InterruptedException | ExecutionException e) {
if (minecraftClient != null) minecraftClient.safeClose();
Expand All @@ -67,7 +65,7 @@ public boolean isConnected(Duration timeout) {
sendRawSilently("ping").get(timeout.toSeconds(), TimeUnit.SECONDS);
return true;
} catch (InterruptedException | ExecutionException | TimeoutException e) {
LOG.error("Lost connection to {}", connectionTuple);
Logger.error("Lost connection to {}", connectionTuple);
safeClose();
return false;
}
Expand Down Expand Up @@ -183,11 +181,11 @@ private void safeClose() {
}

private Future<RconResponse> authenticateClient(String password) {
LOG.debug("Authenticating...");
Logger.debug("Authenticating...");
return sendRaw(RCON_AUTHENTICATION, password, true);
}

private void printCommand(String rawCommand) {
LOG.debug("Sending command: {}", rawCommand);
Logger.debug("Sending command: {}", rawCommand);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import io.graversen.minecraft.rcon.MinecraftClient;
import io.graversen.minecraft.rcon.RconConnectException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tinylog.Logger;

import java.util.concurrent.Callable;

class ConnectTask implements Callable<MinecraftClient> {
private static Logger LOG = LoggerFactory.getLogger(ConnectTask.class);
private final ConnectOptions connectOptions;
private final RconDetails rconDetails;

Expand All @@ -22,13 +20,13 @@ public MinecraftClient call() throws Exception {
int currentAttempt = 1;

while (currentAttempt <= connectOptions.getMaxRetries()) {
LOG.debug("Connection attempt {}", currentAttempt);
Logger.debug("Connection attempt {}", currentAttempt);
currentAttempt++;

try {
return MinecraftClient.connect(rconDetails.getHostname(), rconDetails.getPassword(), rconDetails.getPort());
} catch (Exception e) {
LOG.debug("Connection attempt failed due to: {}", e.getMessage());
Logger.debug("Connection attempt failed due to: {}", e.getMessage());

if (currentAttempt < connectOptions.getMaxRetries() + 1) {
sleep();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import io.graversen.minecraft.rcon.IMinecraftClient;
import io.graversen.minecraft.rcon.MinecraftRcon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tinylog.Logger;

import java.time.Duration;
import java.util.Optional;
Expand All @@ -13,8 +12,6 @@
import java.util.concurrent.TimeUnit;

public class MinecraftRconService implements IMinecraftRconService {
private static Logger LOG = LoggerFactory.getLogger(MinecraftRconService.class);

private final RconDetails rconDetails;
private final ConnectOptions connectOptions;
private final ScheduledExecutorService executorService;
Expand Down Expand Up @@ -77,7 +74,7 @@ public Optional<MinecraftRcon> minecraftRcon() {

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

if (minecraftClient != null) {
Expand Down Expand Up @@ -106,7 +103,7 @@ public boolean onTestConnection() {
public void onPingResult(PingResult pingResult) {
if (!pingResult.isSuccess() && shouldConnect) {
if (isConnected) {
LOG.warn("Connection broken - resetting");
Logger.warn("Connection broken - resetting");
isConnected = false;
minecraftClient = null;
minecraftRcon = null;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/tinylog.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
writer.format = {date: HH:mm:ss.SSS} {level|min-size=5} {pipe} {class-name|min-size=8} {pipe} {message}

0 comments on commit 76aecb3

Please sign in to comment.