Skip to content

Commit

Permalink
Cleaned up logging
Browse files Browse the repository at this point in the history
  • Loading branch information
afisk committed Oct 25, 2009
1 parent 8512a37 commit 6ceb55a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void messageReceived(final ChannelHandlerContext ctx,
final ChannelFutureListener logListener = new ChannelFutureListener() {
public void operationComplete(final ChannelFuture future)
throws Exception {
m_log.warn("Finished writing data");
m_log.info("Finished writing data");
}
};
m_relayChannel.write(msg).addListener(logListener);
Expand All @@ -74,13 +74,13 @@ public void operationComplete(final ChannelFuture future)
public void channelOpen(final ChannelHandlerContext ctx,
final ChannelStateEvent cse) throws Exception {
final Channel ch = cse.getChannel();
m_log.warn("New channel opened from proxy to web: {}", ch);
m_log.info("New channel opened from proxy to web: {}", ch);
}

@Override
public void channelClosed(final ChannelHandlerContext ctx,
final ChannelStateEvent e) throws Exception {
m_log.warn("Got closed event on proxy -> web connection: "+e.getChannel());
m_log.info("Got closed event on proxy -> web connection: "+e.getChannel());
//closeOnFlush(m_browserToProxyChannel);
}

Expand All @@ -98,7 +98,7 @@ public void exceptionCaught(final ChannelHandlerContext ctx,
* Closes the specified channel after all queued write requests are flushed.
*/
private void closeOnFlush(final Channel ch) {
m_log.warn("Closing channel on flush: {}", ch);
m_log.info("Closing channel on flush: {}", ch);
if (ch.isConnected()) {
ch.write(ChannelBuffers.EMPTY_BUFFER).addListener(
ChannelFutureListener.CLOSE);
Expand Down
41 changes: 9 additions & 32 deletions src/main/java/org/littleshoot/proxy/HttpRelayingHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.littleshoot.proxy;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
Expand Down Expand Up @@ -46,59 +45,37 @@ public void messageReceived(final ChannelHandlerContext ctx,
if (!readingChunks) {
final HttpResponse response = (HttpResponse) e.getMessage();

m_log.warn("STATUS: " + response.getStatus());
m_log.warn("VERSION: " + response.getProtocolVersion());
//LOG.warn();

if (!response.getHeaderNames().isEmpty()) {
for (String name: response.getHeaderNames()) {
for (String value: response.getHeaders(name)) {
m_log.warn("HEADER: " + name + " = " + value);
m_log.info("HEADER: " + name + " = " + value);
}
}
//LOG.warn();
}

//if (response.getStatus().getCode() == 200 && response.isChunked()) {
if (response.isChunked()) {
readingChunks = true;
//LOG.warn("CHUNKED CONTENT {");
} else {
ChannelBuffer content = response.getContent();
if (content.readable()) {
//LOG.warn("CONTENT {");
//LOG.warn(content.toString("UTF-8"));
//LOG.warn("} END OF CONTENT");
}
}
}
} else {
final HttpChunk chunk = (HttpChunk) e.getMessage();
if (chunk.isLast()) {
readingChunks = false;
//LOG.warn("} END OF CHUNKED CONTENT");
} else {
//LOG.warn(chunk.getContent().toString("UTF-8"));
//System.out.flush();
}
}
}
if (m_browserToProxyChannel.isOpen()) {
if (!readingChunks) {
//final HttpResponse response = (HttpResponse) e.getMessage();
//LOG.warn("Writing message with body length: "+response.getContent().readableBytes());
}
final ChannelFutureListener logListener = new ChannelFutureListener() {
public void operationComplete(final ChannelFuture future)
throws Exception {
m_log.warn("Finished writing data");
m_log.info("Finished writing data");
}
};

final Object msg = e.getMessage();
m_log.warn("Writing message: {}", msg);
m_log.info("Writing message: {}", msg);
m_browserToProxyChannel.write(msg).addListener(logListener);
}
else {
m_log.warn("Channel not open. Connected? {}",
m_log.info("Channel not open. Connected? {}",
m_browserToProxyChannel.isConnected());
// This will undoubtedly happen anyway, but just in case.
if (e.getChannel().isOpen()) {
Expand All @@ -111,13 +88,13 @@ public void operationComplete(final ChannelFuture future)
public void channelOpen(final ChannelHandlerContext ctx,
final ChannelStateEvent cse) throws Exception {
final Channel ch = cse.getChannel();
m_log.warn("New channel opened from proxy to web: {}", ch);
m_log.info("New channel opened from proxy to web: {}", ch);
}

@Override
public void channelClosed(final ChannelHandlerContext ctx,
final ChannelStateEvent e) throws Exception {
m_log.warn("Got closed event on proxy -> web connection: "+e.getChannel());
m_log.info("Got closed event on proxy -> web connection: "+e.getChannel());
//closeOnFlush(m_browserToProxyChannel);
}

Expand All @@ -135,7 +112,7 @@ public void exceptionCaught(final ChannelHandlerContext ctx,
* Closes the specified channel after all queued write requests are flushed.
*/
private void closeOnFlush(final Channel ch) {
m_log.warn("Closing channel on flush: {}", ch);
m_log.info("Closing channel on flush: {}", ch);
if (ch.isConnected()) {
ch.write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
Expand Down
58 changes: 29 additions & 29 deletions src/main/java/org/littleshoot/proxy/HttpRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ public class HttpRequestHandler extends SimpleChannelUpstreamHandler {
public void messageReceived(final ChannelHandlerContext ctx,
final MessageEvent me) {
m_messagesReceived++;
m_log.warn("Received "+m_messagesReceived+" total messages");
m_log.info("Received "+m_messagesReceived+" total messages");
if (!readingChunks) {
final HttpRequest httpRequest = this.request = (HttpRequest) me.getMessage();

m_log.warn("Got request: {} on channel: "+me.getChannel(), httpRequest);
m_log.info("Got request: {} on channel: "+me.getChannel(), httpRequest);

final Set<String> headerNames = httpRequest.getHeaderNames();
for (final String name : headerNames) {
final List<String> values = httpRequest.getHeaders(name);
m_log.warn(name+": "+values);
m_log.info(name+": "+values);
if (name.equalsIgnoreCase("Proxy-Authorization")) {
final String fullValue = values.iterator().next();
final String value = StringUtils.substringAfter(fullValue, "Basic ").trim();
Expand Down Expand Up @@ -114,21 +114,21 @@ public void messageReceived(final ChannelHandlerContext ctx,
"browser doesn't understand how to supply\n"+
"the credentials required.</p>\n"+
"</body></html>\n";
m_log.warn("Content-Length is really: "+responseBody.length());
m_log.info("Content-Length is really: "+responseBody.length());
writeResponse(ctx.getChannel(), statusLine, headers);
}

else {
m_log.warn("Got proxy authorization!");
m_log.info("Got proxy authorization!");
final String authentication =
httpRequest.getHeader("Proxy-Authorization");
m_log.warn(authentication);
m_log.info(authentication);
httpRequest.removeHeader("Proxy-Authorization");
}

final String uri = httpRequest.getUri();

m_log.warn("Using URI: "+uri);
m_log.info("Using URI: "+uri);
final String noHostUri = ProxyUtils.stripHost(uri);

final HttpMethod method = httpRequest.getMethod();
Expand All @@ -139,11 +139,11 @@ public void messageReceived(final ChannelHandlerContext ctx,
final ChannelBuffer originalContent = httpRequest.getContent();

if (originalContent != null) {
m_log.warn("Setting content");
m_log.info("Setting content");
httpRequestCopy.setContent(originalContent);
}

m_log.warn("Request copy method: {}", httpRequestCopy.getMethod());
m_log.info("Request copy method: {}", httpRequestCopy.getMethod());
for (final String name : headerNames) {
final List<String> values = httpRequest.getHeaders(name);
httpRequestCopy.setHeader(name, values);
Expand Down Expand Up @@ -227,20 +227,20 @@ public void operationComplete(final ChannelFuture future)
public void operationComplete(final ChannelFuture future)
throws Exception {
if (future.isSuccess()) {
m_log.warn("Connected successfully to: {}", future.getChannel());
m_log.info("Connected successfully to: {}", future.getChannel());
final Channel newChannel = cf.getChannel();
newChannel.getCloseFuture().addListener(
new ChannelFutureListener() {
public void operationComplete(
final ChannelFuture closeFuture)
throws Exception {
m_log.warn("Got an outbound channel close event. Removing channel: "+newChannel);
m_log.warn("Channel open??" +newChannel.isOpen());
m_log.info("Got an outbound channel close event. Removing channel: "+newChannel);
m_log.info("Channel open??" +newChannel.isOpen());
m_endpointsToChannelFutures.remove(hostAndPort);
m_log.warn("Outgoing channels on this connection: "+
m_log.info("Outgoing channels on this connection: "+
m_endpointsToChannelFutures.size());
if (m_endpointsToChannelFutures.isEmpty()) {
m_log.warn("All outbound channels closed...");
m_log.info("All outbound channels closed...");

// We *don't* want to close here because
// the external site may have closed
Expand All @@ -251,23 +251,23 @@ public void operationComplete(
// is likely expecting more responses
// on this connection.
if (inboundChannel.isOpen()) {
m_log.warn("Closing on flush...");
m_log.info("Closing on flush...");
closeOnFlush(inboundChannel);
}
}
else {
m_log.warn("Existing connections: {}", m_endpointsToChannelFutures);
m_log.info("Existing connections: {}", m_endpointsToChannelFutures);
}
}
});

m_log.warn("Writing message on channel...");
m_log.info("Writing message on channel...");
final ChannelFuture wf = onConnect.onConnect();
//final ChannelFuture wf = newChannel.write(httpRequestCopy);
wf.addListener(new ChannelFutureListener() {
public void operationComplete(final ChannelFuture wcf)
throws Exception {
m_log.warn("Finished write: "+wcf+ " to: "+
m_log.info("Finished write: "+wcf+ " to: "+
httpRequest.getMethod()+" "+httpRequest.getUri());
}
});
Expand Down Expand Up @@ -335,7 +335,7 @@ private void writeConnectResponse(final ChannelHandlerContext ctx,
private void writeResponse(final Channel browserToProxyChannel,
final String statusLine, final String headers) {
final String fullResponse = statusLine + headers;
m_log.warn("Writing full response:\n"+fullResponse);
m_log.info("Writing full response:\n"+fullResponse);
try {
final ChannelBuffer buf =
ChannelBuffers.copiedBuffer(fullResponse.getBytes("UTF-8"));
Expand Down Expand Up @@ -427,7 +427,7 @@ public ChannelPipeline getPipeline() throws Exception {
cb.setOption("connectTimeoutMillis", 60*1000);

// Start the connection attempt.
m_log.warn("Starting new connection to: "+hostAndPort);
m_log.info("Starting new connection to: "+hostAndPort);
final ChannelFuture future = cb.connect(new InetSocketAddress(host, port));
return future;
}
Expand All @@ -453,34 +453,34 @@ private String parseHostAndPort(final HttpRequest httpRequest) {
else {
hostAndPort = tempUri;
}
m_log.warn("Got URI: "+hostAndPort);
m_log.info("Got URI: "+hostAndPort);
return hostAndPort;
}

@Override
public void channelOpen(final ChannelHandlerContext ctx,
final ChannelStateEvent cse) throws Exception {
final Channel inboundChannel = cse.getChannel();
m_log.warn("New channel opened: {}", inboundChannel);
m_log.info("New channel opened: {}", inboundChannel);
s_totalInboundConnections++;
m_totalInboundConnections++;
m_log.warn("Now "+s_totalInboundConnections+" browser to proxy channels...");
m_log.warn("Now this class has "+m_totalInboundConnections+" browser to proxy channels...");
m_log.info("Now "+s_totalInboundConnections+" browser to proxy channels...");
m_log.info("Now this class has "+m_totalInboundConnections+" browser to proxy channels...");
}

@Override
public void channelClosed(final ChannelHandlerContext ctx,
final ChannelStateEvent cse) {
m_log.warn("Channel closed: {}", cse.getChannel());
m_log.info("Channel closed: {}", cse.getChannel());
s_totalInboundConnections--;
m_totalInboundConnections--;
m_log.warn("Now "+s_totalInboundConnections+" browser to proxy channels...");
m_log.warn("Now this class has "+m_totalInboundConnections+" browser to proxy channels...");
m_log.info("Now "+s_totalInboundConnections+" total browser to proxy channels...");
m_log.info("Now this class has "+m_totalInboundConnections+" browser to proxy channels...");

// The following should always be the case with
// @ChannelPipelineCoverage("one")
if (m_totalInboundConnections == 0) {
m_log.warn("Closing all outgoing channels for this browser connection!!!");
m_log.info("Closing all outgoing channels for this browser connection!!!");
final Collection<ChannelFuture> futures =
this.m_endpointsToChannelFutures.values();
for (final ChannelFuture future : futures) {
Expand All @@ -506,7 +506,7 @@ public void exceptionCaught(final ChannelHandlerContext ctx,
* Closes the specified channel after all queued write requests are flushed.
*/
private static void closeOnFlush(final Channel ch) {
m_log.warn("Closing on flush: {}", ch);
m_log.info("Closing on flush: {}", ch);
if (ch.isConnected()) {
ch.write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
Expand Down

0 comments on commit 6ceb55a

Please sign in to comment.