Skip to content

Commit

Permalink
Logging: use simple console logger
Browse files Browse the repository at this point in the history
We don't actually use any of the functionality of log4j, we just dump
messages straight ot the console.  Simplify packaging requirements by
not using log4j.  (If we need any of the log4j functionality, we can
add an additional log4j backend later.)
  • Loading branch information
ethomson committed Jul 13, 2015
1 parent 1996895 commit 4b2f67b
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 54 deletions.
21 changes: 11 additions & 10 deletions src/main/java/com/microsoft/tfs/tools/poxy/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import java.util.Map;
import java.util.concurrent.ExecutorService;

import org.apache.log4j.Logger;

import com.microsoft.tfs.tools.poxy.handlers.ConnectRequestHandler;
import com.microsoft.tfs.tools.poxy.handlers.DefaultRequestHandler;
import com.microsoft.tfs.tools.poxy.handlers.RequestHandler;
import com.microsoft.tfs.tools.poxy.logger.LogLevel;
import com.microsoft.tfs.tools.poxy.logger.Logger;

/**
* A connection corresponds to one client-to-proxy TCP socket, which is
Expand Down Expand Up @@ -129,7 +129,8 @@ public void run()
*/
if (!keepAlive || requestCount == 0)
{
logger.warn("Connection closed before request could be read on socket "
logger.write(LogLevel.WARNING,
"Connection closed before request could be read on socket "
+ clientToProxySocket);
}
break;
Expand Down Expand Up @@ -157,14 +158,14 @@ else if (!connectionHeaderRead)
catch (SocketException e)
{
// Socket problem so don't try to write an error response
logger.debug("SocketException", e);
logger.write(LogLevel.DEBUG, "SocketException", e);
break;
}
catch (IOException e)
{
// A non-protocol error, but still don't try to write
// an error response
logger.debug("Non protocol exception doing socket IO", e);
logger.write(LogLevel.DEBUG, "Non protocol exception doing socket IO", e);
break;
}

Expand Down Expand Up @@ -208,7 +209,7 @@ else if (request.getMethod().equals(Constants.GET_METHOD)
* The handler was unsuccessful and we should close this
* connection.
*/
logger.debug("Handler " + handler + " was unsuccessful, closing connection");
logger.write(LogLevel.DEBUG, "Handler " + handler + " was unsuccessful, closing connection");

// Best effort flush
try
Expand Down Expand Up @@ -239,7 +240,7 @@ else if (request.getMethod().equals(Constants.GET_METHOD)
&& response.getContentLengthHeaderValue() != -1
&& response.getContentLengthHeaderValue() != response.getActualResponseBodyLength())
{
logger.warn(MessageFormat.format(
logger.write(LogLevel.WARNING, MessageFormat.format(
"Header Content-Length {0} != {1} actually written bytes",
response.getContentLengthHeaderValue(),
response.getActualResponseBodyLength()));
Expand All @@ -256,15 +257,15 @@ else if (request.getMethod().equals(Constants.GET_METHOD)
}
catch (SocketTimeoutException e)
{
logger.debug("Read timeout on " + clientToProxySocket);
logger.write(LogLevel.DEBUG, "Read timeout on " + clientToProxySocket);
}
catch (IOException e)
{
logger.debug("IOException on socket " + clientToProxySocket, e);
logger.write(LogLevel.DEBUG, "IOException on socket " + clientToProxySocket, e);
}
catch (Exception e)
{
logger.warn("Unhandled exception on socket " + clientToProxySocket, e);
logger.write(LogLevel.WARNING, "Unhandled exception on socket " + clientToProxySocket, e);
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/microsoft/tfs/tools/poxy/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

package com.microsoft.tfs.tools.poxy;

import org.apache.log4j.Logger;
import com.microsoft.tfs.tools.poxy.logger.Logger;

public class Header
{
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/microsoft/tfs/tools/poxy/HeaderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import java.util.List;

import com.microsoft.tfs.tools.poxy.logger.LogLevel;

public abstract class HeaderUtils
{
/*
Expand Down Expand Up @@ -83,7 +85,7 @@ public static long getContentLength(List<Header> headers)
}
catch (NumberFormatException e)
{
Header.logger.warn("Couldn't parse content length header: " + h);
Header.logger.write(LogLevel.WARNING, "Couldn't parse content length header: " + h);
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/microsoft/tfs/tools/poxy/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import com.microsoft.tfs.tools.poxy.logger.LogLevel;
import com.microsoft.tfs.tools.poxy.logger.Logger;

public class IOUtils
{
Expand Down Expand Up @@ -106,7 +107,7 @@ public static List<Header> readHeaders(final InputStream input)
}

final Header h = new Header(line);
logger.trace(h.getName() + ": " + h.getValue());
logger.write(LogLevel.TRACE, h.getName() + ": " + h.getValue());
ret.add(h);
}

Expand All @@ -126,7 +127,7 @@ public static void close(final Socket socket)
}
catch (IOException e)
{
logger.debug("Error closing socket", e);
logger.write(LogLevel.DEBUG, "Error closing socket", e);
}
}

Expand Down Expand Up @@ -195,7 +196,7 @@ public static void copyChunkedStream(InputStream input, OutputStream output)

if (size == 0)
{
logger.trace("Got last chunk");
logger.write(LogLevel.TRACE, "Got last chunk");

// Should be one CRLF after the last chunk
readLine(input);
Expand All @@ -204,7 +205,7 @@ public static void copyChunkedStream(InputStream input, OutputStream output)
break;
}

logger.trace("Copying chunk of " + size + " bytes");
logger.write(LogLevel.TRACE, "Copying chunk of " + size + " bytes");
copyStream(input, output, size);

// Should be one CRLF after the data
Expand Down
21 changes: 8 additions & 13 deletions src/main/java/com/microsoft/tfs/tools/poxy/Poxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

import com.microsoft.tfs.tools.poxy.GetOptions.Option;
import com.microsoft.tfs.tools.poxy.GetOptions.OptionException;
import com.microsoft.tfs.tools.poxy.logger.LogLevel;
import com.microsoft.tfs.tools.poxy.logger.Logger;

public class Poxy
{
Expand Down Expand Up @@ -71,7 +67,7 @@ public void run()
}
catch (IOException e)
{
logger.fatal("Could not start server", e);
logger.write(LogLevel.FATAL, "Could not start server", e);
System.exit(1);
}
}
Expand All @@ -84,9 +80,8 @@ public void run()
*/
private Options getOptionsAndConfigureLogging()
{
// Configure log4j with basic console logging at INFO
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d{ISO8601} %-5p [%t] %c{1} - %m%n")));
Logger.getRootLogger().setLevel(Level.INFO);
// Configure our logger at the INFO level
Logger.setLevel(LogLevel.INFO);

/* Setup command-line options (with defaults) */
final Option[] availableOptions =
Expand Down Expand Up @@ -140,8 +135,8 @@ private Options getOptionsAndConfigureLogging()
// Debug
if (getOptions.getArguments().get("debug") != null)
{
Logger.getRootLogger().setLevel(Level.DEBUG);
logger.debug("Log level set to " + Level.DEBUG);
Logger.setLevel(LogLevel.DEBUG);
logger.write(LogLevel.DEBUG, "Log level set to " + LogLevel.DEBUG);
}

// Integer options
Expand Down Expand Up @@ -193,7 +188,7 @@ private Options getOptionsAndConfigureLogging()
proxyOptions.setProxyCredentials(getOptions.getArguments("credentials"));
}

logger.info("Starting server on port " + Integer.toString(proxyOptions.getLocalPort()));
logger.write(LogLevel.INFO, "Starting server on port " + Integer.toString(proxyOptions.getLocalPort()));

return proxyOptions;
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/microsoft/tfs/tools/poxy/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import com.microsoft.tfs.tools.poxy.logger.LogLevel;
import com.microsoft.tfs.tools.poxy.logger.Logger;

public class Request
{
Expand Down Expand Up @@ -114,7 +115,7 @@ private boolean readRequestLine()
parseURI(parts[1]);
parseVersion(parts[2]);

logger.debug(method + " " + uri + " " + version);
logger.write(LogLevel.DEBUG, method + " " + uri + " " + version);

return true;
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/microsoft/tfs/tools/poxy/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import com.microsoft.tfs.tools.poxy.logger.LogLevel;
import com.microsoft.tfs.tools.poxy.logger.Logger;

/**
* Writes an HTTP response. Offers methods for writing lines which terminate
Expand Down Expand Up @@ -130,7 +131,7 @@ public void writeStatus(int status, String message, String httpVersion)
MessageFormat.format("{0} {1} {2}", httpVersion, Integer.toString(status), message != null ? message
: Status.NAMES.get(status));

logger.debug(s);
logger.write(LogLevel.DEBUG, s);
writeLine(s);
}

Expand Down Expand Up @@ -160,7 +161,7 @@ public void writeHeader(Header h)
}
catch (NumberFormatException e)
{
logger.warn("Couldn't parse content length " + h.getValue() + " as Long", e);
logger.write(LogLevel.WARNING, "Couldn't parse content length " + h.getValue() + " as Long", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.log4j.Logger;

import com.microsoft.tfs.tools.poxy.Connection;
import com.microsoft.tfs.tools.poxy.HTTPException;
import com.microsoft.tfs.tools.poxy.Header;
Expand All @@ -32,6 +30,8 @@
import com.microsoft.tfs.tools.poxy.Response;
import com.microsoft.tfs.tools.poxy.Status;
import com.microsoft.tfs.tools.poxy.UTF8Utils;
import com.microsoft.tfs.tools.poxy.logger.LogLevel;
import com.microsoft.tfs.tools.poxy.logger.Logger;

public class ConnectRequestHandler
extends RequestHandler
Expand Down Expand Up @@ -112,11 +112,11 @@ private Object getFutureResult(final Future<?> future)
}
catch (InterruptedException e)
{
logger.warn("Interrupted waiting on IO thread", e);
logger.write(LogLevel.WARNING, "Interrupted waiting on IO thread", e);
}
catch (ExecutionException e)
{
logger.warn("Execution exception in IO thread", e);
logger.write(LogLevel.WARNING, "Execution exception in IO thread", e);
}

return null;
Expand Down Expand Up @@ -174,7 +174,7 @@ private Socket connectViaProxy(URI forwardProxyURI, InetSocketAddress address, L
throw new HTTPException("Connection closed by " + address);
}

logger.debug("Forward proxy responds: " + statusLine);
logger.write(LogLevel.DEBUG, "Forward proxy responds: " + statusLine);

final String[] parts = statusLine.split(" ", 3);
// Need at least 2; message is optional
Expand Down Expand Up @@ -309,12 +309,12 @@ public void run()
*
* Log at a low level because this is common.
*/
logger.trace("SocketException", e);
logger.write(LogLevel.TRACE, "SocketException", e);
}
catch (IOException e)
{
// Unlikely to get a non-SocketException
logger.info("Non socket exception doing socket IO", e);
logger.write(LogLevel.INFO, "Non socket exception doing socket IO", e);
}
finally
{
Expand Down
Loading

0 comments on commit 4b2f67b

Please sign in to comment.