From 7a1746281dd980785e967acb10b87df6bdc2c4a3 Mon Sep 17 00:00:00 2001 From: Gravita Date: Mon, 10 May 2021 14:51:52 +0700 Subject: [PATCH] [REFACTOR] Moving to log4j2 logger finally --- .../handlers/NettyServerSocketHandler.java | 7 +++-- .../socket/handlers/NettyWebAPIHandler.java | 5 ++- .../handlers/WebSocketFrameHandler.java | 31 +++++++++---------- .../fileserver/FileServerHandler.java | 5 +-- .../socket/response/auth/AuthResponse.java | 11 +++---- .../response/auth/CheckServerResponse.java | 9 +++--- .../response/auth/JoinServerResponse.java | 11 ++++--- 7 files changed, 40 insertions(+), 39 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/NettyServerSocketHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/NettyServerSocketHandler.java index f239d548..290e2379 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/NettyServerSocketHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/NettyServerSocketHandler.java @@ -1,5 +1,7 @@ package pro.gravit.launchserver.socket.handlers; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.config.LaunchServerConfig; import pro.gravit.launchserver.socket.LauncherNettyServer; @@ -8,11 +10,12 @@ import javax.net.ssl.SSLServerSocketFactory; import java.net.InetSocketAddress; -// TODO refactor @SuppressWarnings("unused") public final class NettyServerSocketHandler implements Runnable, AutoCloseable { private transient final LaunchServer server; + @Deprecated public volatile boolean logConnections = Boolean.getBoolean("launcher.logConnections"); + private transient final Logger logger = LogManager.getLogger(); public LauncherNettyServer nettyServer; private SSLServerSocketFactory ssf; @@ -30,7 +33,7 @@ public void close() { @Override public void run() { - LogHelper.info("Starting netty server socket thread"); + logger.info("Starting netty server socket thread"); nettyServer = new LauncherNettyServer(server); for (LaunchServerConfig.NettyBindAddress address : server.config.netty.binds) { nettyServer.bind(new InetSocketAddress(address.address, address.port)); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/NettyWebAPIHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/NettyWebAPIHandler.java index 0cd2eb28..284eb5d1 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/NettyWebAPIHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/NettyWebAPIHandler.java @@ -5,6 +5,8 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.*; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import pro.gravit.launcher.Launcher; import pro.gravit.launchserver.socket.NettyConnectContext; import pro.gravit.utils.helper.LogHelper; @@ -22,6 +24,7 @@ public class NettyWebAPIHandler extends SimpleChannelInboundHandler { private static final TreeSet severletList = new TreeSet<>(Comparator.comparingInt((e) -> -e.key.length())); private final NettyConnectContext context; + private transient final Logger logger = LogManager.getLogger(); public NettyWebAPIHandler(NettyConnectContext context) { super(); @@ -52,7 +55,7 @@ protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) thro try { pair.callback.handle(ctx, msg, context); } catch (Throwable e) { - LogHelper.error(e); + logger.error(e); ctx.writeAndFlush(new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer("Internal Server Error 500".getBytes())), ctx.voidPromise()); } isNext = false; diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java index bcbc64b0..e66733c1 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java @@ -5,6 +5,8 @@ import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.websocketx.*; import io.netty.util.concurrent.ScheduledFuture; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.socket.Client; import pro.gravit.launchserver.socket.NettyConnectContext; @@ -22,6 +24,7 @@ public class WebSocketFrameHandler extends SimpleChannelInboundHandler hooks = new BiHookSet<>(); private final UUID connectUUID = UUID.randomUUID(); + private transient final Logger logger = LogManager.getLogger(); public NettyConnectContext context; private Client client; private ScheduledFuture future; @@ -48,9 +51,7 @@ public final UUID getConnectUUID() { @Override public void channelActive(ChannelHandlerContext ctx) { - if (LogHelper.isDevEnabled()) { - LogHelper.dev("New client %s", IOHelper.getIP(ctx.channel().remoteAddress())); - } + logger.trace("New client {}", IOHelper.getIP(ctx.channel().remoteAddress())); client = new Client(null); Channel ch = ctx.channel(); service.registerClient(ch); @@ -63,20 +64,16 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) { try { if (hooks.hook(ctx, frame)) return; } catch (Throwable ex) { - LogHelper.error(ex); + logger.error(ex); } if (frame instanceof TextWebSocketFrame) { try { service.process(ctx, (TextWebSocketFrame) frame, client, context.ip); } catch (Throwable ex) { - if (LogHelper.isDebugEnabled()) { - LogHelper.warning("Client %s send invalid request. Connection force closed.", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip); - if (LogHelper.isDevEnabled()) { - LogHelper.dev("Client message: %s", ((TextWebSocketFrame) frame).text()); - } - if (LogHelper.isStacktraceEnabled()) { - LogHelper.error(ex); - } + logger.warn("Client {} send invalid request. Connection force closed.", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip); + if(logger.isTraceEnabled()) { + logger.trace("Client message: {}", ((TextWebSocketFrame) frame).text()); + logger.error(ex); } ctx.channel().close(); } @@ -85,28 +82,28 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) { ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content())); //return; } else if ((frame instanceof PongWebSocketFrame)) { - LogHelper.dev("WebSocket Client received pong"); + logger.trace("WebSocket Client received pong"); } else if ((frame instanceof CloseWebSocketFrame)) { int statusCode = ((CloseWebSocketFrame) frame).statusCode(); ctx.channel().close(); } else { String message = "unsupported frame type: " + frame.getClass().getName(); - LogHelper.error(new UnsupportedOperationException(message)); // prevent strange crash here. + logger.error(new UnsupportedOperationException(message)); // prevent strange crash here. } } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { if (future != null) future.cancel(true); - if (LogHelper.isDevEnabled()) { - LogHelper.dev("Client %s disconnected", IOHelper.getIP(ctx.channel().remoteAddress())); + if(logger.isTraceEnabled()) { + logger.trace("Client {} disconnected", IOHelper.getIP(ctx.channel().remoteAddress())); } int refCount = client.refCount.decrementAndGet(); if (client.session != null) { if (refCount == 0) { srv.sessionManager.addClient(client); } else if (refCount < 0) { - LogHelper.warning("Client session %s reference counter invalid - %d", client.session, refCount); + logger.warn("Client session {} reference counter invalid - {}", client.session, refCount); } } super.channelInactive(ctx); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/fileserver/FileServerHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/fileserver/FileServerHandler.java index 8c9f38fc..d33cd775 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/fileserver/FileServerHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/fileserver/FileServerHandler.java @@ -229,10 +229,7 @@ public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) thr sendNotModified(ctx); return; } - } catch (UnsupportedTemporalTypeException e) { - if (LogHelper.isDebugEnabled()) { - LogHelper.warning("Request access If-Modifed-Since: %s not parsed correctly", ifModifiedSince); - } + } catch (UnsupportedTemporalTypeException ignored) { } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/AuthResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/AuthResponse.java index d548f05b..693d573f 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/AuthResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/AuthResponse.java @@ -1,6 +1,8 @@ package pro.gravit.launchserver.socket.response.auth; import io.netty.channel.ChannelHandlerContext; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import pro.gravit.launcher.events.request.AuthRequestEvent; import pro.gravit.launcher.request.auth.AuthRequest; import pro.gravit.launcher.request.auth.password.*; @@ -27,7 +29,7 @@ import java.util.UUID; public class AuthResponse extends SimpleResponse { - public final transient static Random random = new SecureRandom(); + private transient final Logger logger = LogManager.getLogger(); public String login; public String client; public boolean getSession; @@ -52,9 +54,6 @@ public void execute(ChannelHandlerContext ctx, Client clientData) throws Excepti } if (clientData.isAuth) { - if (LogHelper.isDevEnabled()) { - LogHelper.warning("Client %s double auth", clientData.username == null ? ip : clientData.username); - } sendError("You are already logged in"); return; } @@ -113,8 +112,8 @@ public void execute(ChannelHandlerContext ctx, Client clientData) throws Excepti } if (authType == ConnectTypes.CLIENT && server.config.protectHandler.allowGetAccessToken(context)) { clientData.uuid = pair.handler.auth(aresult); - if (LogHelper.isDebugEnabled()) { - LogHelper.debug("Auth: %s accessToken %s uuid: %s", login, result.accessToken, clientData.uuid.toString()); + if (logger.isDebugEnabled()) { + logger.debug("Auth: {} accessToken {} uuid: {}", login, result.accessToken, clientData.uuid.toString()); } } else { clientData.uuid = pair.handler.usernameToUUID(aresult.username); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/CheckServerResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/CheckServerResponse.java index 3303dfa6..d44667d9 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/CheckServerResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/CheckServerResponse.java @@ -1,6 +1,8 @@ package pro.gravit.launchserver.socket.response.auth; import io.netty.channel.ChannelHandlerContext; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import pro.gravit.launcher.events.request.CheckServerRequestEvent; import pro.gravit.launchserver.auth.AuthException; import pro.gravit.launchserver.socket.Client; @@ -13,6 +15,7 @@ public class CheckServerResponse extends SimpleResponse { public String serverID; public String username; public String client; + private transient final Logger logger = LogManager.getLogger(); @Override public String getType() { @@ -31,14 +34,12 @@ public void execute(ChannelHandlerContext ctx, Client pClient) { result.uuid = pClient.auth.handler.checkServer(username, serverID); if (result.uuid != null) result.playerProfile = ProfileByUUIDResponse.getProfile(result.uuid, username, client, pClient.auth.textureProvider); - if (LogHelper.isDebugEnabled()) { - LogHelper.debug("checkServer: %s uuid: %s serverID: %s", result.playerProfile.username, result.uuid.toString(), serverID); - } + logger.debug("checkServer: {} uuid: {} serverID: {}", result.playerProfile.username, result.uuid, serverID); } catch (AuthException | HookException e) { sendError(e.getMessage()); return; } catch (Exception e) { - LogHelper.error(e); + logger.error(e); sendError("Internal authHandler error"); return; } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/JoinServerResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/JoinServerResponse.java index d3da4bfc..537083a4 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/JoinServerResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/JoinServerResponse.java @@ -1,6 +1,8 @@ package pro.gravit.launchserver.socket.response.auth; import io.netty.channel.ChannelHandlerContext; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import pro.gravit.launcher.events.request.JoinServerRequestEvent; import pro.gravit.launchserver.auth.AuthException; import pro.gravit.launchserver.auth.protect.interfaces.JoinServerProtectHandler; @@ -13,6 +15,7 @@ public class JoinServerResponse extends SimpleResponse { public String serverID; public String accessToken; public String username; + private transient final Logger logger = LogManager.getLogger(); @Override public String getType() { @@ -40,17 +43,15 @@ public void execute(ChannelHandlerContext ctx, Client client) { } } if (client.auth == null) { - LogHelper.warning("Client auth is null. Using default."); + logger.warn("Client auth is null. Using default."); success = server.config.getAuthProviderPair().handler.joinServer(username, accessToken, serverID); } else success = client.auth.handler.joinServer(username, accessToken, serverID); - if (LogHelper.isDebugEnabled()) { - LogHelper.debug("joinServer: %s accessToken: %s serverID: %s", username, accessToken, serverID); - } + logger.debug("joinServer: {} accessToken: {} serverID: {}", username, accessToken, serverID); } catch (AuthException | HookException | SecurityException e) { sendError(e.getMessage()); return; } catch (Exception e) { - LogHelper.error(e); + logger.error(e); sendError("Internal authHandler error"); return; }