You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2019/05/19 10:14:03 UTC

[httpcomponents-core] 01/04: Consistent logging of i/o session details

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch bug-fixes
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit 581c7038eeb10a4b4948578d23007a17fc3c3d6a
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sun May 19 11:54:03 2019 +0200

    Consistent logging of i/o session details
---
 .../testing/nio/LoggingIOSessionListener.java      | 31 +++++++++++++++-------
 1 file changed, 22 insertions(+), 9 deletions(-)

diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingIOSessionListener.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingIOSessionListener.java
index 74e66f7..6a785ca 100644
--- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingIOSessionListener.java
+++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingIOSessionListener.java
@@ -28,11 +28,12 @@
 package org.apache.hc.core5.testing.nio;
 
 import org.apache.hc.core5.http.ConnectionClosedException;
+import org.apache.hc.core5.net.InetAddressUtils;
 import org.apache.hc.core5.reactor.IOSession;
 import org.apache.hc.core5.reactor.IOSessionListener;
 import org.apache.hc.core5.testing.classic.LoggingSupport;
-import org.slf4j.LoggerFactory;
 import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class LoggingIOSessionListener implements IOSessionListener {
 
@@ -46,49 +47,49 @@ public class LoggingIOSessionListener implements IOSessionListener {
     @Override
     public void tlsStarted(final IOSession session) {
         if (connLog.isDebugEnabled()) {
-            connLog.debug(LoggingSupport.getId(session) + " TLS session started: " + session);
+            connLog.debug(LoggingSupport.getId(session) + " TLS session started: " + formatSession(session));
         }
     }
 
     @Override
     public void tlsInbound(final IOSession session) {
         if (connLog.isDebugEnabled()) {
-            connLog.debug(LoggingSupport.getId(session) + " TLS inbound: " + session);
+            connLog.debug(LoggingSupport.getId(session) + " TLS inbound: " + formatSession(session));
         }
     }
 
     @Override
     public void tlsOutbound(final IOSession session) {
         if (connLog.isDebugEnabled()) {
-            connLog.debug(LoggingSupport.getId(session) + " TLS outbound: " + session);
+            connLog.debug(LoggingSupport.getId(session) + " TLS outbound: " + formatSession(session));
         }
     }
 
     @Override
     public void connected(final IOSession session) {
         if (connLog.isDebugEnabled()) {
-            connLog.debug(LoggingSupport.getId(session) + " connected: " + session);
+            connLog.debug(LoggingSupport.getId(session) + " connected: " + formatSession(session));
         }
     }
 
     @Override
     public void inputReady(final IOSession session) {
         if (connLog.isDebugEnabled()) {
-            connLog.debug(LoggingSupport.getId(session) + " input ready: " + session);
+            connLog.debug(LoggingSupport.getId(session) + " input ready: " + formatSession(session));
         }
     }
 
     @Override
     public void outputReady(final IOSession session) {
         if (connLog.isDebugEnabled()) {
-            connLog.debug(LoggingSupport.getId(session) + " output ready: " + session);
+            connLog.debug(LoggingSupport.getId(session) + " output ready: " + formatSession(session));
         }
     }
 
     @Override
     public void timeout(final IOSession session) {
         if (connLog.isDebugEnabled()) {
-            connLog.debug(LoggingSupport.getId(session) + " timeout: " + session);
+            connLog.debug(LoggingSupport.getId(session) + " timeout: " + formatSession(session));
         }
     }
 
@@ -103,8 +104,20 @@ public class LoggingIOSessionListener implements IOSessionListener {
     @Override
     public void disconnected(final IOSession session) {
         if (connLog.isDebugEnabled()) {
-            connLog.debug(LoggingSupport.getId(session) + " disconnected: " + session);
+            connLog.debug(LoggingSupport.getId(session) + " disconnected");
+        }
+    }
+
+    private static String formatSession(final IOSession session) {
+        final StringBuilder buffer = new StringBuilder(90);
+        if (session.isClosed()) {
+            buffer.append("closed");
+        } else {
+            InetAddressUtils.formatAddress(buffer, session.getLocalAddress());
+            buffer.append("<->");
+            InetAddressUtils.formatAddress(buffer, session.getRemoteAddress());
         }
+        return buffer.toString();
     }
 
 }