hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1128849 - in /httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio: client/NHttpClientProtocolHandler.java conn/DefaultClientConnection.java conn/LoggingIOSession.java conn/Wire.java
Date Sun, 29 May 2011 09:55:29 GMT
Author: olegk
Date: Sun May 29 09:55:28 2011
New Revision: 1128849

URL: http://svn.apache.org/viewvc?rev=1128849&view=rev
Log:
Improved logging: add connection ID to connection event logs

Modified:
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/NHttpClientProtocolHandler.java
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/DefaultClientConnection.java
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/LoggingIOSession.java
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/Wire.java

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/NHttpClientProtocolHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/NHttpClientProtocolHandler.java?rev=1128849&r1=1128848&r2=1128849&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/NHttpClientProtocolHandler.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/NHttpClientProtocolHandler.java
Sun May 29 09:55:28 2011
@@ -33,7 +33,6 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpException;
-import org.apache.http.HttpInetConnection;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
@@ -89,7 +88,7 @@ class NHttpClientProtocolHandler impleme
         HttpExchange httpexchange = new HttpExchange();
         HttpContext context = conn.getContext();
         if (this.log.isDebugEnabled()) {
-            this.log.debug("Connected " + formatState(conn, httpexchange));
+            this.log.debug(conn + ": Connected");
         }
         context.setAttribute(HTTP_EXCHNAGE, httpexchange);
         requestReady(conn);
@@ -97,10 +96,9 @@ class NHttpClientProtocolHandler impleme
 
     public void closed(final NHttpClientConnection conn) {
         HttpContext context = conn.getContext();
-        HttpExchange httpexchange = getHttpExchange(context);
         HttpAsyncExchangeHandler<?> handler = getHandler(context);
         if (this.log.isDebugEnabled()) {
-            this.log.debug("Disconnected " + formatState(conn, httpexchange));
+            this.log.debug(conn + ": Disconnected");
         }
         if (handler != null) {
             handler.cancel();
@@ -110,7 +108,9 @@ class NHttpClientProtocolHandler impleme
     public void exception(final NHttpClientConnection conn, final HttpException ex) {
         HttpContext context = conn.getContext();
         HttpAsyncExchangeHandler<?> handler = getHandler(context);
-        this.log.error("HTTP protocol exception: " + ex.getMessage(), ex);
+        if (this.log.isErrorEnabled()) {
+            this.log.error(conn + " HTTP protocol exception: " + ex.getMessage(), ex);
+        }
         if (handler != null) {
             handler.failed(ex);
         }
@@ -120,7 +120,9 @@ class NHttpClientProtocolHandler impleme
     public void exception(final NHttpClientConnection conn, final IOException ex) {
         HttpContext context = conn.getContext();
         HttpAsyncExchangeHandler<?> handler = getHandler(context);
-        this.log.error("I/O error: " + ex.getMessage(), ex);
+        if (this.log.isErrorEnabled()) {
+            this.log.error(conn + " I/O error: " + ex.getMessage(), ex);
+        }
         if (handler != null) {
             handler.failed(ex);
         }
@@ -132,14 +134,14 @@ class NHttpClientProtocolHandler impleme
         HttpExchange httpexchange = getHttpExchange(context);
         HttpAsyncExchangeHandler<?> handler = getHandler(context);
         if (this.log.isDebugEnabled()) {
-            this.log.debug("Request ready " + formatState(conn, httpexchange));
+            this.log.debug(conn + " Request ready");
         }
         if (httpexchange.getRequestState() != MessageState.READY) {
             return;
         }
         if (handler == null || handler.isDone()) {
             if (this.log.isDebugEnabled()) {
-                this.log.debug("No request submitted " + formatState(conn, httpexchange));
+                this.log.debug(conn + " No request submitted");
             }
             return;
         }
@@ -170,13 +172,13 @@ class NHttpClientProtocolHandler impleme
             }
         } catch (IOException ex) {
             if (this.log.isDebugEnabled()) {
-                this.log.debug("I/O error: " + ex.getMessage(), ex);
+                this.log.debug(conn + " I/O error: " + ex.getMessage(), ex);
             }
             shutdownConnection(conn);
             handler.failed(ex);
         } catch (HttpException ex) {
             if (this.log.isDebugEnabled()) {
-                this.log.debug("HTTP protocol exception: " + ex.getMessage(), ex);
+                this.log.debug(conn + " HTTP protocol exception: " + ex.getMessage(), ex);
             }
             closeConnection(conn);
             handler.failed(ex);
@@ -188,7 +190,7 @@ class NHttpClientProtocolHandler impleme
         HttpExchange httpexchange = getHttpExchange(context);
         HttpAsyncExchangeHandler<?> handler = getHandler(context);
         if (this.log.isDebugEnabled()) {
-            this.log.debug("Input ready " + formatState(conn, httpexchange));
+            this.log.debug(conn + " Input ready");
         }
         try {
             handler.consumeContent(decoder, conn);
@@ -209,7 +211,7 @@ class NHttpClientProtocolHandler impleme
         HttpExchange httpexchange = getHttpExchange(context);
         HttpAsyncExchangeHandler<?> handler = getHandler(context);
         if (this.log.isDebugEnabled()) {
-            this.log.debug("Output ready " + formatState(conn, httpexchange));
+            this.log.debug(conn + " Output ready");
         }
         try {
             if (httpexchange.getRequestState() == MessageState.ACK) {
@@ -222,7 +224,7 @@ class NHttpClientProtocolHandler impleme
             }
         } catch (IOException ex) {
             if (this.log.isDebugEnabled()) {
-                this.log.debug("I/O error: " + ex.getMessage(), ex);
+                this.log.debug(conn + " I/O error: " + ex.getMessage(), ex);
             }
             shutdownConnection(conn);
             handler.failed(ex);
@@ -234,7 +236,7 @@ class NHttpClientProtocolHandler impleme
         HttpExchange httpexchange = getHttpExchange(context);
         HttpAsyncExchangeHandler<?> handler = getHandler(context);
         if (this.log.isDebugEnabled()) {
-            this.log.debug("Response received " + formatState(conn, httpexchange));
+            this.log.debug(conn + " Response received");
         }
         try {
             HttpResponse response = conn.getHttpResponse();
@@ -292,7 +294,7 @@ class NHttpClientProtocolHandler impleme
         HttpExchange httpexchange = getHttpExchange(context);
         HttpAsyncExchangeHandler<?> handler = getHandler(context);
         if (this.log.isDebugEnabled()) {
-            this.log.debug("Timeout " + formatState(conn, httpexchange));
+            this.log.debug(conn + " Timeout");
         }
         try {
             if (httpexchange.getRequestState() == MessageState.ACK) {
@@ -352,7 +354,7 @@ class NHttpClientProtocolHandler impleme
             }
         }
         if (this.log.isDebugEnabled()) {
-            this.log.debug("Response processed " + formatState(conn, httpexchange));
+            this.log.debug(conn + " Response processed");
         }
         handler.responseCompleted();
         httpexchange.reset();
@@ -375,35 +377,4 @@ class NHttpClientProtocolHandler impleme
             && status != HttpStatus.SC_RESET_CONTENT;
     }
 
-    private String formatState(final NHttpConnection conn, final HttpExchange httpexchange)
{
-        StringBuilder buf = new StringBuilder();
-        buf.append("[");
-        if (conn.isOpen() && (conn instanceof HttpInetConnection)) {
-            HttpInetConnection inetconn = (HttpInetConnection) conn;
-            buf.append(inetconn.getRemoteAddress());
-            buf.append(":");
-            buf.append(inetconn.getRemotePort());
-        }
-        buf.append("(");
-        buf.append(conn.isOpen() ? "open" : "closed");
-        buf.append("),request=");
-        buf.append(httpexchange.getRequestState());
-        if (httpexchange.getRequest() != null) {
-            buf.append("(");
-            buf.append(httpexchange.getRequest().getRequestLine());
-            buf.append(")");
-        }
-        buf.append(",response=");
-        buf.append(httpexchange.getResponseState());
-        if (httpexchange.getResponse() != null) {
-            buf.append("(");
-            buf.append(httpexchange.getResponse().getStatusLine());
-            buf.append(")");
-        }
-        buf.append(",valid=");
-        buf.append(httpexchange.isValid());
-        buf.append("]");
-        return buf.toString();
-    }
-
 }

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/DefaultClientConnection.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/DefaultClientConnection.java?rev=1128849&r1=1128848&r2=1128849&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/DefaultClientConnection.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/DefaultClientConnection.java
Sun May 29 09:55:28 2011
@@ -28,6 +28,7 @@ package org.apache.http.impl.nio.conn;
 
 import java.io.IOException;
 import java.nio.channels.ReadableByteChannel;
+import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -54,6 +55,9 @@ public class DefaultClientConnection
     private final Log wirelog   = LogFactory.getLog("org.apache.http.wire");
     private final Log log;
 
+    private static final AtomicLong COUNT = new AtomicLong();
+
+    private String id;
     private SSLIOSession ssliosession;
 
     public DefaultClientConnection(
@@ -62,9 +66,10 @@ public class DefaultClientConnection
             final ByteBufferAllocator allocator,
             final HttpParams params) {
         super(iosession, responseFactory, allocator, params);
+        this.id = "http-outgoing-" + COUNT.incrementAndGet();
         this.log = LogFactory.getLog(iosession.getClass());
         if (this.log.isDebugEnabled() || this.wirelog.isDebugEnabled()) {
-            this.session = new LoggingIOSession(iosession, this.log, this.wirelog, "http-outgoing");
+            this.session = new LoggingIOSession(iosession, this.id, this.log, this.wirelog);
         }
         if (iosession instanceof SSLIOSession) {
             this.ssliosession = (SSLIOSession) iosession;
@@ -76,8 +81,8 @@ public class DefaultClientConnection
     public void upgrade(final IOSession iosession) {
         this.session.setBufferStatus(null);
         if (this.log.isDebugEnabled() || this.wirelog.isDebugEnabled()) {
-            this.session = new LoggingIOSession(iosession, this.headerlog, this.wirelog,
-                    iosession instanceof SSLIOSession ? "https-outgoing" : "http-outgoing");
+            this.log.debug(this.id + " Upgrade session " + iosession);
+            this.session = new LoggingIOSession(iosession, this.id, this.headerlog, this.wirelog);
         } else {
             this.session = iosession;
         }
@@ -110,6 +115,11 @@ public class DefaultClientConnection
                 super.createResponseParser(buffer, responseFactory, params));
     }
 
+    @Override
+    public String toString() {
+        return this.id;
+    }
+
     class LoggingNHttpMessageWriter implements NHttpMessageWriter<HttpRequest> {
 
         private final NHttpMessageWriter<HttpRequest> writer;
@@ -125,10 +135,10 @@ public class DefaultClientConnection
 
         public void write(final HttpRequest request) throws IOException, HttpException {
             if (request != null && headerlog.isDebugEnabled()) {
-                headerlog.debug(">> " + request.getRequestLine().toString());
+                headerlog.debug(id + " >> " + request.getRequestLine().toString());
                 Header[] headers = request.getAllHeaders();
                 for (int i = 0; i < headers.length; i++) {
-                    headerlog.debug(">> " + headers[i].toString());
+                    headerlog.debug(id + " >> " + headers[i].toString());
                 }
             }
             this.writer.write(request);
@@ -157,10 +167,10 @@ public class DefaultClientConnection
             HttpResponse response = this.parser.parse();
             if (headerlog.isDebugEnabled()) {
                 if (response != null && headerlog.isDebugEnabled()) {
-                    headerlog.debug("<< " + response.getStatusLine().toString());
+                    headerlog.debug(id + " << " + response.getStatusLine().toString());
                     Header[] headers = response.getAllHeaders();
                     for (int i = 0; i < headers.length; i++) {
-                        headerlog.debug("<< " + headers[i].toString());
+                        headerlog.debug(id + " << " + headers[i].toString());
                     }
                 }
             }

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/LoggingIOSession.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/LoggingIOSession.java?rev=1128849&r1=1128848&r2=1128849&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/LoggingIOSession.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/LoggingIOSession.java
Sun May 29 09:55:28 2011
@@ -32,7 +32,6 @@ import java.net.SocketAddress;
 import java.nio.ByteBuffer;
 import java.nio.channels.ByteChannel;
 import java.nio.channels.SelectionKey;
-import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.commons.logging.Log;
 import org.apache.http.nio.reactor.IOSession;
@@ -40,24 +39,22 @@ import org.apache.http.nio.reactor.Sessi
 
 public class LoggingIOSession implements IOSession {
 
-    private static AtomicLong COUNT = new AtomicLong(0);
-
-    private final Log log;
-    private final Wire wirelog;
     private final IOSession session;
     private final ByteChannel channel;
     private final String id;
+    private final Log log;
+    private final Wire wirelog;
 
-    public LoggingIOSession(final IOSession session, final Log log, final Log wirelog, final
String id) {
+    public LoggingIOSession(final IOSession session, final String id, final Log log, final
Log wirelog) {
         super();
         if (session == null) {
             throw new IllegalArgumentException("I/O session may not be null");
         }
-        this.log = log;
-        this.wirelog = new Wire(wirelog);
         this.session = session;
         this.channel = new LoggingByteChannel();
-        this.id = id + "-" + COUNT.incrementAndGet();
+        this.id = id;
+        this.log = log;
+        this.wirelog = new Wire(wirelog, this.id);
     }
 
     public ByteChannel channel() {
@@ -77,7 +74,7 @@ public class LoggingIOSession implements
     }
 
     private static String formatOps(int ops) {
-        StringBuffer buffer = new StringBuffer(6);
+        StringBuilder buffer = new StringBuilder(6);
         buffer.append('[');
         if ((ops & SelectionKey.OP_READ) > 0) {
             buffer.append('r');
@@ -96,32 +93,29 @@ public class LoggingIOSession implements
     }
 
     public void setEventMask(int ops) {
+        this.session.setEventMask(ops);
         if (this.log.isDebugEnabled()) {
-            this.log.debug("I/O session " + this.id + " " + this.session + ": Set event mask
"
-                    + formatOps(ops));
+            this.log.debug(this.id + " " + this.session + ": Event mask set " + formatOps(ops));
         }
-        this.session.setEventMask(ops);
     }
 
     public void setEvent(int op) {
+        this.session.setEvent(op);
         if (this.log.isDebugEnabled()) {
-            this.log.debug("I/O session " + this.id + " " + this.session + ": Set event "
-                    + formatOps(op));
+            this.log.debug(this.id + " " + this.session + ": Event set " + formatOps(op));
         }
-        this.session.setEvent(op);
     }
 
     public void clearEvent(int op) {
+        this.session.clearEvent(op);
         if (this.log.isDebugEnabled()) {
-            this.log.debug("I/O session " + this.id + " " + this.session + ": Clear event
"
-                    + formatOps(op));
+            this.log.debug(this.id + " " + this.session + ": Event cleared " + formatOps(op));
         }
-        this.session.clearEvent(op);
     }
 
     public void close() {
         if (this.log.isDebugEnabled()) {
-            this.log.debug("I/O session " + this.id + " " + this.session + ": Close");
+            this.log.debug(this.id + " " + this.session + ": Close");
         }
         this.session.close();
     }
@@ -136,7 +130,7 @@ public class LoggingIOSession implements
 
     public void shutdown() {
         if (this.log.isDebugEnabled()) {
-            this.log.debug("I/O session " + this.id + " " + this.session + ": Shutdown");
+            this.log.debug(this.id + " " + this.session + ": Shutdown");
         }
         this.session.shutdown();
     }
@@ -147,8 +141,7 @@ public class LoggingIOSession implements
 
     public void setSocketTimeout(int timeout) {
         if (this.log.isDebugEnabled()) {
-            this.log.debug("I/O session " + this.id + " " + this.session + ": Set timeout
"
-                    + timeout);
+            this.log.debug(this.id + " " + this.session + ": Set timeout " + timeout);
         }
         this.session.setSocketTimeout(timeout);
     }
@@ -171,26 +164,29 @@ public class LoggingIOSession implements
 
     public void setAttribute(final String name, final Object obj) {
         if (this.log.isDebugEnabled()) {
-            this.log.debug("I/O session " + this.id + " " + this.session + ": Set attribute
"
-                    + name);
+            this.log.debug(this.id + " " + this.session + ": Set attribute " + name);
         }
         this.session.setAttribute(name, obj);
     }
 
     public Object removeAttribute(final String name) {
         if (this.log.isDebugEnabled()) {
-            this.log.debug("I/O session " + this.id + " " + this.session + ": Remove attribute
"
-                    + name);
+            this.log.debug(this.id + " " + this.session + ": Remove attribute " + name);
         }
         return this.session.removeAttribute(name);
     }
 
+    @Override
+    public String toString() {
+        return this.id + " " + this.session.toString();
+    }
+
     class LoggingByteChannel implements ByteChannel {
 
         public int read(final ByteBuffer dst) throws IOException {
             int bytesRead = session.channel().read(dst);
             if (log.isDebugEnabled()) {
-                log.debug("I/O session " + id + " " + session + ": " + bytesRead + " bytes
read");
+                log.debug(id + " " + session + ": " + bytesRead + " bytes read");
             }
             if (bytesRead > 0 && wirelog.isEnabled()) {
                 ByteBuffer b = dst.duplicate();
@@ -205,7 +201,7 @@ public class LoggingIOSession implements
         public int write(final ByteBuffer src) throws IOException {
             int byteWritten = session.channel().write(src);
             if (log.isDebugEnabled()) {
-                log.debug("I/O session " + id + " " + session + ": " + byteWritten + " bytes
written");
+                log.debug(id + " " + session + ": " + byteWritten + " bytes written");
             }
             if (byteWritten > 0 && wirelog.isEnabled()) {
                 ByteBuffer b = src.duplicate();
@@ -219,7 +215,7 @@ public class LoggingIOSession implements
 
         public void close() throws IOException {
             if (log.isDebugEnabled()) {
-                log.debug("I/O session " + id + " " + session + ": Channel close");
+                log.debug(id + " " + session + ": Channel close");
             }
             session.channel().close();
         }

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/Wire.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/Wire.java?rev=1128849&r1=1128848&r2=1128849&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/Wire.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/Wire.java
Sun May 29 09:55:28 2011
@@ -33,10 +33,12 @@ import org.apache.commons.logging.Log;
 public class Wire {
 
     private final Log log;
+    private final String id;
 
-    public Wire(final Log log) {
+    public Wire(final Log log, final String id) {
         super();
         this.log = log;
+        this.id = id;
     }
 
     private void wire(final String header, final byte[] b, int pos, int off) {
@@ -49,7 +51,7 @@ public class Wire {
                     buffer.append("[\\n]\"");
                     buffer.insert(0, "\"");
                     buffer.insert(0, header);
-                    this.log.debug(buffer.toString());
+                    this.log.debug(this.id + " " + buffer.toString());
                     buffer.setLength(0);
             } else if ((ch < 32) || (ch > 127)) {
                 buffer.append("[0x");
@@ -63,7 +65,7 @@ public class Wire {
             buffer.append('\"');
             buffer.insert(0, '\"');
             buffer.insert(0, header);
-            this.log.debug(buffer.toString());
+            this.log.debug(this.id + " " + buffer.toString());
         }
     }
 



Mime
View raw message