From c9af231c7e34a2d7d0e2012527d142bea8578329 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 27 Jul 2018 01:54:40 -0400 Subject: [PATCH] Switch to log4j2 logging --- .gitignore | 3 +++ build.gradle | 2 ++ .../network/ConnectionManager.java | 18 ++++++++------- .../client/LoginSessionHandler.java | 7 ++++-- src/main/resources/log4j2.xml | 23 +++++++++++++++++++ 5 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 src/main/resources/log4j2.xml diff --git a/.gitignore b/.gitignore index b140a9666..1fe58326e 100644 --- a/.gitignore +++ b/.gitignore @@ -118,3 +118,6 @@ gradle-app.setting # End of https://www.gitignore.io/api/java,gradle,intellij + +# Other trash +logs/ \ No newline at end of file diff --git a/build.gradle b/build.gradle index e94b59934..04ac38b7f 100644 --- a/build.gradle +++ b/build.gradle @@ -25,6 +25,8 @@ dependencies { compile "io.netty:netty-transport-native-epoll:${nettyVersion}" compile "io.netty:netty-transport-native-epoll:${nettyVersion}:linux-x86_64" compile 'net.kyori:text:1.12-1.5.0' + compile 'org.apache.logging.log4j:log4j-api:2.11.0' + compile 'org.apache.logging.log4j:log4j-core:2.11.0' testCompile 'org.junit.jupiter:junit-jupiter-api:5.3.0-M1' testCompile 'org.junit.jupiter:junit-jupiter-engine:5.3.0-M1' } diff --git a/src/main/java/com/velocitypowered/network/ConnectionManager.java b/src/main/java/com/velocitypowered/network/ConnectionManager.java index 2e455e680..867bf8e85 100644 --- a/src/main/java/com/velocitypowered/network/ConnectionManager.java +++ b/src/main/java/com/velocitypowered/network/ConnectionManager.java @@ -11,6 +11,8 @@ import com.velocitypowered.proxy.protocol.netty.MinecraftDecoder; import com.velocitypowered.proxy.protocol.netty.MinecraftEncoder; import com.velocitypowered.proxy.protocol.netty.MinecraftVarintFrameDecoder; import com.velocitypowered.proxy.protocol.netty.MinecraftVarintLengthEncoder; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.LogManager; import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Channel; @@ -45,6 +47,8 @@ import static com.velocitypowered.network.Connections.MINECRAFT_ENCODER; import static com.velocitypowered.network.Connections.READ_TIMEOUT; public final class ConnectionManager { + private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(ConnectionManager.class); + private static final String DISABLE_EPOLL_PROPERTY = "velocity.connection.disable-epoll"; private static final boolean DISABLE_EPOLL = Boolean.getBoolean(DISABLE_EPOLL_PROPERTY); private final Set endpoints = new HashSet<>(); @@ -71,12 +75,12 @@ public final class ConnectionManager { private void logChannelInformation(final boolean epoll) { final StringBuilder sb = new StringBuilder(); - sb.append("Channel type: "); + sb.append("Using channel type "); sb.append(epoll ? "epoll": "nio"); if(DISABLE_EPOLL) { sb.append(String.format(" - epoll explicitly disabled using -D%s=true", DISABLE_EPOLL_PROPERTY)); } - System.out.println(sb.toString()); // TODO: move to logger + logger.info(sb.toString()); // TODO: move to logger } public void bind(final InetSocketAddress address) { @@ -109,10 +113,9 @@ public final class ConnectionManager { final Channel channel = future.channel(); if (future.isSuccess()) { this.endpoints.add(channel); - System.out.println("Listening on " + channel.localAddress()); // TODO: move to logger + logger.info("Listening on {}", channel.localAddress()); } else { - System.out.println("Can't bind to " + channel.localAddress()); // TODO: move to logger - future.cause().printStackTrace(); + logger.error("Can't bind to {}", address, future.cause()); } }); } @@ -126,11 +129,10 @@ public final class ConnectionManager { public void shutdown() { for (final Channel endpoint : this.endpoints) { try { - System.out.println(String.format("Closing endpoint %s", endpoint.localAddress())); // TODO: move to logger + logger.info("Closing endpoint {}", endpoint.localAddress()); endpoint.close().sync(); } catch (final InterruptedException e) { - System.err.println("Interrupted whilst closing endpoint"); // TODO: move to logger - e.printStackTrace(); + logger.info("Interrupted whilst closing endpoint", e); } } } diff --git a/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java b/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java index c89b69d65..04f226507 100644 --- a/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java +++ b/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java @@ -12,6 +12,8 @@ import com.velocitypowered.proxy.connection.backend.ServerConnection; import com.velocitypowered.proxy.data.ServerInfo; import com.velocitypowered.proxy.util.EncryptionUtils; import com.velocitypowered.proxy.util.UuidUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.net.InetSocketAddress; import java.net.URL; @@ -21,6 +23,8 @@ import java.util.Arrays; import java.util.concurrent.ThreadLocalRandom; public class LoginSessionHandler implements MinecraftSessionHandler { + private static final Logger logger = LogManager.getLogger(LoginSessionHandler.class); + private static final String MOJANG_SERVER_AUTH_URL = "https://sessionserver.mojang.com/session/minecraft/hasJoined?username=%s&serverId=%s&ip=%s"; @@ -71,8 +75,7 @@ public class LoginSessionHandler implements MinecraftSessionHandler { handleSuccessfulLogin(profile); }, inbound.getChannel().eventLoop()) .exceptionally(exception -> { - System.out.println("Can't enable encryption"); - exception.printStackTrace(); + logger.error("Unable to enable encryption", exception); inbound.close(); return null; }); diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml new file mode 100644 index 000000000..bddab2847 --- /dev/null +++ b/src/main/resources/log4j2.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file