446564 Refactored RequestLog Mechanism

reverted API
This commit is contained in:
Greg Wilkins 2015-04-22 10:37:39 +10:00
parent bcd4e5fc45
commit 9eb2cb4c0f
11 changed files with 57 additions and 13 deletions

View File

@ -98,10 +98,13 @@ public abstract class AbstractNCSARequestLog extends AbstractLifeCycle implement
* int, long)
*/
@Override
public void log(Request request, int status, long written)
public void log(Request request, Response response)
{
try
{
int status = response.getCommittedMetaData().getStatus();
long written = response.getHttpChannel().getBytesWritten();
if (_ignorePathMap != null && _ignorePathMap.getMatch(request.getRequestURI()) != null)
return;

View File

@ -110,6 +110,11 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor
return _state;
}
public long getBytesWritten()
{
return _written;
}
/**
* @return the number of requests handled by this connection
*/
@ -138,7 +143,7 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor
_requestLog = requestLog;
}
public MetaData.Response getCommittedInfo()
public MetaData.Response getCommittedMetaData()
{
return _committedMetaData;
}
@ -522,7 +527,8 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor
public void onCompleted()
{
if (_requestLog!=null )
_requestLog.log(_request, _committedMetaData == null ? -1 : _committedMetaData.getStatus(), _written);
_requestLog.log(_request, _response);
_transport.onCompleted();
}

View File

@ -38,7 +38,7 @@ import org.eclipse.jetty.util.annotation.ManagedObject;
* formats.
*/
@ManagedObject("NCSA standard format request log")
public class NCSARequestLog extends AbstractNCSARequestLog implements RequestLog
public class NCSARequestLog extends AbstractNCSARequestLog
{
private String _filename;
private boolean _append;

View File

@ -28,5 +28,14 @@ import org.eclipse.jetty.server.handler.RequestLogHandler;
*/
public interface RequestLog
{
public void log(Request request, int status, long written);
/* ------------------------------------------------------------ */
/**
* @param request The request to log.
* @param response The response to log. Note that for some requests
* the response instance may not have been fully populated (Eg 400 bad request
* responses are sent without a servlet response object). Thus for basic
* log information it is best to consult {@link Response#getCommittedMetaData()}
* and {@link Response#getHttpChannel()} directly.
*/
public void log(Request request, Response response);
}

View File

@ -121,7 +121,7 @@ public class Response implements HttpServletResponse
_out = out;
}
protected HttpChannel getHttpChannel()
public HttpChannel getHttpChannel()
{
return _channel;
}
@ -1236,6 +1236,22 @@ public class Response implements HttpServletResponse
return new MetaData.Response(_channel.getRequest().getHttpVersion(), getStatus(), getReason(), _fields, getLongContentLength());
}
/** Get the MetaData.Response committed for this response.
* This may differ from the meta data in this response for
* exceptional responses (eg 4xx and 5xx responses generated
* by the container) and the committedMetaData should be used
* for logging purposes.
* @return The committed MetaData or a {@link #newResponseMetaData()}
* if not yet committed.
*/
public MetaData.Response getCommittedMetaData()
{
MetaData.Response meta = _channel.getCommittedMetaData();
if (meta==null)
return newResponseMetaData();
return meta;
}
@Override
public boolean isCommitted()
{

View File

@ -27,7 +27,7 @@ import org.eclipse.jetty.util.log.Slf4jLog;
* Implementation of NCSARequestLog where output is sent as a SLF4J INFO Log message on the named logger "org.eclipse.jetty.server.RequestLog"
*/
@ManagedObject("NCSA standard format request log to slf4j bridge")
public class Slf4jRequestLog extends AbstractNCSARequestLog implements RequestLog
public class Slf4jRequestLog extends AbstractNCSARequestLog
{
private Slf4jLog logger;
private String loggerName;

View File

@ -43,6 +43,7 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.IO;
@ -71,8 +72,9 @@ public class BadRequestLogHandlerTest
public List<String> captured = new ArrayList<>();
@Override
public void log(Request request, int status, long written)
public void log(Request request, Response response)
{
int status = response.getCommittedMetaData().getStatus();
captured.add(String.format("%s %s %s %03d",request.getMethod(),request.getHttpURI(),request.getProtocol(),status));
}
}

View File

@ -77,8 +77,9 @@ public class RequestLogHandlerTest
public List<String> captured = new ArrayList<>();
@Override
public void log(Request request, int status, long written)
public void log(Request request, Response response)
{
int status = response.getCommittedMetaData().getStatus();
captured.add(String.format("%s %s %s %03d",request.getMethod(),request.getRequestURI(),request.getProtocol(),status));
}
}

View File

@ -45,6 +45,7 @@ import javax.servlet.http.HttpServletResponseWrapper;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.RequestLogHandler;
@ -887,8 +888,10 @@ public class AsyncServletTest
class Log extends AbstractLifeCycle implements RequestLog
{
@Override
public void log(Request request, int status, long written)
public void log(Request request, Response response)
{
int status = response.getCommittedMetaData().getStatus();
long written = response.getHttpChannel().getBytesWritten();
_log.add(status+" "+written+" "+request.getRequestURI());
}
}

View File

@ -77,8 +77,9 @@ public class ServletRequestLogTest
public List<String> captured = new ArrayList<>();
@Override
public void log(Request request, int status, long written)
public void log(Request request, Response response)
{
int status = response.getCommittedMetaData().getStatus();
captured.add(String.format("%s %s %s %03d",request.getMethod(),request.getRequestURI(),request.getProtocol(),status));
}
}

View File

@ -26,6 +26,7 @@ import java.util.List;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.RequestLogHandler;
@ -193,8 +194,10 @@ public class ContinuationTest extends ContinuationBase
class Log extends AbstractLifeCycle implements RequestLog
{
@Override
public void log(Request request, int status, long written)
public void log(Request request, Response response)
{
int status = response.getCommittedMetaData().getStatus();
long written = response.getHttpChannel().getBytesWritten();
_log.add(status+" "+written+" "+request.getRequestURI());
}