From 2f5b0da83de728d7ba1b35b3528055462002de8a Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 24 Jun 2016 15:18:24 +0300 Subject: logging --- src/main/java/com/juick/jabber/ws/Main.java | 10 +++++++--- src/main/java/com/juick/jabber/ws/WSData.java | 20 ++++++++++---------- src/main/java/com/juick/jabber/ws/WSKeepAlive.java | 9 ++++++--- .../java/com/juick/jabber/ws/XMPPConnection.java | 13 +++++++++---- 4 files changed, 32 insertions(+), 20 deletions(-) (limited to 'src/main/java/com/juick/jabber') diff --git a/src/main/java/com/juick/jabber/ws/Main.java b/src/main/java/com/juick/jabber/ws/Main.java index 3a26c3ad..6f5ed96a 100644 --- a/src/main/java/com/juick/jabber/ws/Main.java +++ b/src/main/java/com/juick/jabber/ws/Main.java @@ -21,10 +21,11 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; import java.io.FileInputStream; -import java.sql.DriverManager; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Properties; +import java.util.logging.Level; +import java.util.logging.LogManager; +import java.util.logging.Logger; /** * @@ -32,6 +33,8 @@ import java.util.Properties; */ public class Main { + private static final Logger logger = Logger.getLogger("Websockets"); + JdbcTemplate sql; XMPPConnection xmpp; public static String STATSFILE = null; @@ -43,6 +46,7 @@ public class Main { public void start() { try { + LogManager.getLogManager().readConfiguration(new FileInputStream("/etc/juick/ws_logging.properties")); Properties conf = new Properties(); conf.load(new FileInputStream("/etc/juick/ws.conf")); @@ -56,7 +60,7 @@ public class Main { new Thread(new WSData(sql)).start(); new Thread(new WSKeepAlive(sql)).start(); } catch (Exception e) { - System.err.println(e); + logger.log(Level.SEVERE, "websockets initialization error", e); } } diff --git a/src/main/java/com/juick/jabber/ws/WSData.java b/src/main/java/com/juick/jabber/ws/WSData.java index 3abc63d8..9ae98bcf 100644 --- a/src/main/java/com/juick/jabber/ws/WSData.java +++ b/src/main/java/com/juick/jabber/ws/WSData.java @@ -17,13 +17,16 @@ import java.nio.charset.Charset; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Iterator; +import java.util.logging.Level; +import java.util.logging.Logger; /** * * @author ugnich */ public class WSData implements Runnable { - + + private static final Logger logger = Logger.getLogger("Websockets"); static final String WEBSOCKET_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; JdbcTemplate sql; public Selector sel; @@ -53,7 +56,7 @@ public class WSData implements Runnable { SocketChannel sChannel = ssChannel.accept(); sChannel.configureBlocking(false); sChannel.register(sel, SelectionKey.OP_READ); - System.out.println(sChannel.socket().getRemoteSocketAddress().toString() + " ACCEPTED"); + logger.info(sChannel.socket().getRemoteSocketAddress().toString() + " ACCEPTED"); } else if (selKey.isReadable()) { SocketChannel sChannel = (SocketChannel) selKey.channel(); ByteBuffer buf = ByteBuffer.allocate(10240); @@ -63,7 +66,6 @@ public class WSData implements Runnable { buf.flip(); CharBuffer charbuf = Charset.forName("ISO-8859-1").decode(buf); - System.out.println("DATA: " + charbuf.toString()); buf.rewind(); switch (buf.get(0)) { @@ -89,7 +91,7 @@ public class WSData implements Runnable { throw new IOException(sChannel.socket().getRemoteSocketAddress().toString() + " END OF STREAM"); } } catch (IOException e) { - System.err.println("WSData: " + e); + logger.log(Level.SEVERE, "websocket exception", e); sChannel.socket().close(); sChannel.close(); selKey.cancel(); @@ -98,7 +100,7 @@ public class WSData implements Runnable { } } } catch (Exception e) { - System.err.println("WSData: " + e); + logger.log(Level.SEVERE, "websocket exception", e); } } @@ -224,7 +226,7 @@ public class WSData implements Runnable { sock.write(out); if (responseCode == 101) { - System.out.println(sock.socket().getRemoteSocketAddress().toString() + " HANDSHAKE (VUID = " + VUID + "; MID = " + MID + ")"); + logger.info(sock.socket().getRemoteSocketAddress().toString() + " HANDSHAKE (VUID = " + VUID + "; MID = " + MID + ")"); } else { throw new IOException(sock.socket().getRemoteSocketAddress().toString() + " " + responseCode); } @@ -236,7 +238,7 @@ public class WSData implements Runnable { MessageDigest md = MessageDigest.getInstance("SHA-1"); return Base64.encode(md.digest(base.getBytes())); } catch (NoSuchAlgorithmException e) { - System.err.println("calcHeaderAccept: " + e); + logger.severe("calcHeaderAccept: " + e); } return ""; } @@ -264,9 +266,7 @@ public class WSData implements Runnable { public void updateSocketTS(SocketChannel sock) { synchronized (Main.clients) { - Iterator i = Main.clients.iterator(); - while (i.hasNext()) { - SocketSubscribed s = i.next(); + for (SocketSubscribed s : Main.clients) { if (s.sock == sock) { s.tsLastData = System.currentTimeMillis(); break; diff --git a/src/main/java/com/juick/jabber/ws/WSKeepAlive.java b/src/main/java/com/juick/jabber/ws/WSKeepAlive.java index f253439d..6244013d 100644 --- a/src/main/java/com/juick/jabber/ws/WSKeepAlive.java +++ b/src/main/java/com/juick/jabber/ws/WSKeepAlive.java @@ -5,12 +5,15 @@ import org.springframework.jdbc.core.JdbcTemplate; import java.io.PrintWriter; import java.nio.ByteBuffer; import java.util.Iterator; +import java.util.logging.Level; +import java.util.logging.Logger; /** * * @author ugnich */ public class WSKeepAlive implements Runnable { + private static final Logger logger = Logger.getLogger(WSKeepAlive.class.getName()); JdbcTemplate sql; ByteBuffer pingBytes; @@ -44,7 +47,7 @@ public class WSKeepAlive implements Runnable { statsFile = new PrintWriter(Main.STATSFILE, "UTF-8"); } catch (Exception e) { statsFile = null; - System.err.println("WSKeepAlive statsFile: " + e); + logger.severe("WSKeepAlive statsFile: " + e); } } @@ -69,7 +72,7 @@ public class WSKeepAlive implements Runnable { statsFile.print("" + (s.allMessages ? "+" : "") + ""); statsFile.print("" + (s.allReplies ? "+" : "") + ""); } catch (Exception e) { - System.err.println("WSKeepAlive statsFile print: " + e); + logger.log(Level.SEVERE, "WSKeepAlive statsFile print", e); } } @@ -90,7 +93,7 @@ public class WSKeepAlive implements Runnable { statsFile.write(""); statsFile.close(); } catch (Exception e) { - System.err.println("WSKeepAlive statsFile close: " + e); + logger.log(Level.SEVERE, "WSKeepAlive statsFile close", e); } } diff --git a/src/main/java/com/juick/jabber/ws/XMPPConnection.java b/src/main/java/com/juick/jabber/ws/XMPPConnection.java index 1679ae8f..8c187a36 100644 --- a/src/main/java/com/juick/jabber/ws/XMPPConnection.java +++ b/src/main/java/com/juick/jabber/ws/XMPPConnection.java @@ -17,6 +17,8 @@ import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.Iterator; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.stream.Collectors; /** @@ -24,14 +26,17 @@ import java.util.stream.Collectors; * @author ugnich */ public class XMPPConnection implements Runnable, Stream.StreamListener, Message.MessageListener { + private static final Logger logger = Logger.getLogger(XMPPConnection.class.getName()); JdbcTemplate sql; Stream xmpp; String xmppPassword; + MessageSerializer ms; public XMPPConnection(JdbcTemplate sql, String password) { this.sql = sql; xmppPassword = password; + ms = new MessageSerializer(); } @Override @@ -44,25 +49,25 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message. xmpp.addListener((Message.MessageListener) this); xmpp.startParsing(); } catch (IOException e) { - System.err.println("XMPPConnection: " + e); + logger.log(Level.SEVERE, "XMPPConnection error", e); } } @Override public void onStreamReady() { - System.err.println("Stream ready"); + logger.info("XMPP stream ready"); } @Override public void onStreamFail(String msg) { - System.err.println("Stream failed: " + msg); + logger.severe("XMPP stream failed: " + msg); } @Override public void onMessage(com.juick.xmpp.Message msg) { JuickMessage jmsg = (JuickMessage) msg.getChild(JuickMessage.XMLNS); if (jmsg != null) { - System.err.println("MID=" + jmsg.getMID() + "; RID=" + jmsg.getRID()); + logger.info("got jmsg: " + ms.serialize(jmsg).toString()); if (jmsg.getMID() == 0) { int uid_to = 0; try { -- cgit v1.2.3