From 1d837a835c04078ec66325e9fb9de21ac401874e Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Wed, 30 Nov 2016 15:10:15 +0700 Subject: prevent string concatenation for disabled loggers --- .../src/main/java/com/juick/ws/WebsocketComponent.java | 18 +++++++++--------- .../src/main/java/com/juick/ws/XMPPConnection.java | 13 +++++++------ 2 files changed, 16 insertions(+), 15 deletions(-) (limited to 'juick-ws/src/main/java/com') diff --git a/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java b/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java index 831d16f1..adb39bb2 100644 --- a/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java +++ b/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java @@ -52,7 +52,7 @@ public class WebsocketComponent extends TextWebSocketHandler { visitor = userService.getUserByHash(hash); } else { try { - logger.info(String.format("wrong hash for %d from %s", visitor.getUid(), hXRealIP)); + logger.info("wrong hash for {} from {}", visitor.getUid(), hXRealIP); session.close(new CloseStatus(403, "Forbidden")); } catch (IOException e) { logger.warn("ws error", e); @@ -61,19 +61,19 @@ public class WebsocketComponent extends TextWebSocketHandler { break; } } - logger.info(String.format("user %d connected to %s from %s", visitor.getUid(), hLocation.getPath(), hXRealIP)); + logger.info("user {} connected to {} from {}", visitor.getUid(), hLocation.getPath(), hXRealIP); int MID = 0; SocketSubscribed sockSubscr = null; if (hLocation.getPath().equals("/")) { - logger.info(String.format("user %d connected", visitor.getUid())); + logger.info("user {} connected", visitor.getUid()); sockSubscr = new SocketSubscribed(session, hXRealIP, visitor, false); } else if (hLocation.getPath().equals("/_all")) { - logger.info(String.format("user %d connected to legacy _all (%s)", visitor.getUid(), hLocation.getPath())); + logger.info("user {} connected to legacy _all ({})", visitor.getUid(), hLocation.getPath()); sockSubscr = new SocketSubscribed(session, hXRealIP, visitor, true); sockSubscr.allMessages = true; } else if (hLocation.getPath().equals("/_replies")) { - logger.info(String.format("user %d connected to legacy _replies (%s)", visitor.getUid(), hLocation.getPath())); + logger.info("user {} connected to legacy _replies ({})", visitor.getUid(), hLocation.getPath()); sockSubscr = new SocketSubscribed(session, hXRealIP, visitor, true); sockSubscr.allReplies = true; } else if (hLocation.getPath().matches("/\\d+$")) { @@ -83,7 +83,7 @@ public class WebsocketComponent extends TextWebSocketHandler { } if (MID > 0) { if (messagesService.canViewThread(MID, visitor.getUid())) { - logger.info(String.format("user %d connected to legacy thread (%d) from %s", visitor.getUid(), MID, hXRealIP)); + logger.info("user {} connected to legacy thread ({}) from {}", visitor.getUid(), MID, hXRealIP); sockSubscr = new SocketSubscribed(session, hXRealIP, visitor, true); sockSubscr.MID = MID; } else { @@ -98,7 +98,7 @@ public class WebsocketComponent extends TextWebSocketHandler { if (sockSubscr != null) { synchronized (clients) { clients.add(sockSubscr); - logger.info(clients.size() + " clients connected"); + logger.info("{} clients connected", clients.size()); } } } @@ -106,14 +106,14 @@ public class WebsocketComponent extends TextWebSocketHandler { @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { synchronized (clients) { - logger.info(String.format("session closed with status %d: %s", status.getCode(), status.getReason())); + logger.info("session closed with status {}: {}", status.getCode(), status.getReason()); clients.removeIf(c -> { if (c.session.getId().equals(session.getId())) { return true; } return false; }); - logger.info(clients.size() + " clients connected"); + logger.info("{} clients connected", clients.size()); } } diff --git a/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java b/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java index b75ca998..f535d123 100644 --- a/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java +++ b/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java @@ -68,7 +68,8 @@ public class XMPPConnection implements AutoCloseable { Message msg = e.getMessage(); com.juick.Message jmsg = msg.getExtension(com.juick.Message.class); if (jmsg != null) { - logger.info("got msg: " + ms.writeValueAsString(jmsg)); + if (logger.isInfoEnabled()) // prevent writeValueAsString execution if log is disabled + logger.info("got msg: {}", ms.writeValueAsString(jmsg)); if (jmsg.getMid() == 0) { int uid_to = NumberUtils.toInt(msg.getTo().getLocal(), 0); if (uid_to > 0) { @@ -106,7 +107,7 @@ public class XMPPConnection implements AutoCloseable { synchronized (wsHandler.getClients()) { wsHandler.getClients().stream().filter(c -> !c.legacy && c.visitor.getUid() == uid_to).forEach(c -> { try { - logger.info("sending pm to " + c.visitor.getUid()); + logger.info("sending pm to {}", c.visitor.getUid()); c.session.sendMessage(new TextMessage(json)); } catch (IOException e) { logger.warn("ws error", e); @@ -125,7 +126,7 @@ public class XMPPConnection implements AutoCloseable { || (!c.legacy && uids.contains(c.visitor.getUid()))) // subscriptions .forEach(c -> { try { - logger.info("sending message to " + c.visitor.getUid()); + logger.info("sending message to {}", c.visitor.getUid()); c.session.sendMessage(new TextMessage(json)); } catch (IOException e) { logger.warn("ws error", e); @@ -135,7 +136,7 @@ public class XMPPConnection implements AutoCloseable { c.legacy && c.allMessages) // legacy all posts .forEach(c -> { try { - logger.info("sending message to legacy client " + c.visitor.getUid()); + logger.info("sending message to legacy client {}", c.visitor.getUid()); c.session.sendMessage(new TextMessage(json)); } catch (IOException e) { logger.warn("ws error", e); @@ -155,7 +156,7 @@ public class XMPPConnection implements AutoCloseable { || (!c.legacy && threadUsers.contains(c.visitor.getUid()))) // subscriptions .forEach(c -> { try { - logger.info("sending reply to " + c.visitor.getUid()); + logger.info("sending reply to {}", c.visitor.getUid()); c.session.sendMessage(new TextMessage(json)); } catch (IOException e) { logger.warn("ws error", e); @@ -165,7 +166,7 @@ public class XMPPConnection implements AutoCloseable { (c.legacy && c.allReplies) || (c.legacy && c.MID == jmsg.getMid())) // legacy replies .forEach(c -> { try { - logger.info("sending reply to legacy client " + c.visitor.getUid()); + logger.info("sending reply to legacy client {}", c.visitor.getUid()); c.session.sendMessage(new TextMessage(json)); } catch (IOException e) { logger.warn("ws error", e); -- cgit v1.2.3