Skip to content

Commit

Permalink
446564 Refactored RequestLog Mechanism
Browse files Browse the repository at this point in the history
RequestLog can now be set on the HttpChannel and is called during the onCommitted callback.
The RequestLog may be set either on the server or via an updated RequestLogHandler that can
be applied to a specific context.
  • Loading branch information
gregw committed Oct 31, 2014
1 parent 947a872 commit e3bda4e
Show file tree
Hide file tree
Showing 10 changed files with 368 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public void onPushRequest(MetaData.Request request)
@Override
protected void commit(MetaData.Response info)
{
super.commit(info);
if (LOG.isDebugEnabled())
{
LOG.debug("HTTP2 Commit Response #{}/{}:{}{} {} {}{}{}",
Expand Down
34 changes: 13 additions & 21 deletions jetty-server/src/main/config/etc/jetty-requestlog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,18 @@
<Configure id="Server" class="org.eclipse.jetty.server.Server">

<!-- =========================================================== -->
<!-- Configure Request Log -->
<!-- Configure Request Log for Server -->
<!-- (Use RequestLogHandler for a context specific RequestLog -->
<!-- =========================================================== -->
<Ref refid="Handlers">
<Call name="addHandler">
<Arg>
<New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler">
<Set name="requestLog">
<New id="RequestLogImpl" class="org.eclipse.jetty.server.AsyncNCSARequestLog">
<Set name="filename"><Property name="jetty.base" default="." /><Property name="requestlog.filename" default="/logs/yyyy_mm_dd.request.log"/></Set>
<Set name="filenameDateFormat"><Property name="requestlog.filenameDateFormat" default="yyyy_MM_dd"/></Set>
<Set name="retainDays"><Property name="requestlog.retain" default="90"/></Set>
<Set name="append"><Property name="requestlog.append" default="false"/></Set>
<Set name="extended"><Property name="requestlog.extended" default="false"/></Set>
<Set name="logCookies"><Property name="requestlog.cookies" default="false"/></Set>
<Set name="LogTimeZone"><Property name="requestlog.timezone" default="GMT"/></Set>
</New>
</Set>
</New>
</Arg>
</Call>
</Ref>

<Set name="RequestLog">
<New id="RequestLog" class="org.eclipse.jetty.server.AsyncNCSARequestLog">
<Set name="filename"><Property name="jetty.base" default="." /><Property name="requestlog.filename" default="/logs/yyyy_mm_dd.request.log"/></Set>
<Set name="filenameDateFormat"><Property name="requestlog.filenameDateFormat" default="yyyy_MM_dd"/></Set>
<Set name="retainDays"><Property name="requestlog.retain" default="90"/></Set>
<Set name="append"><Property name="requestlog.append" default="false"/></Set>
<Set name="extended"><Property name="requestlog.extended" default="false"/></Set>
<Set name="logCookies"><Property name="requestlog.cookies" default="false"/></Set>
<Set name="LogTimeZone"><Property name="requestlog.timezone" default="GMT"/></Set>
</New>
</Set>
</Configure>
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@ protected StringBuilder initialValue()
public abstract void write(String requestEntry) throws IOException;

/* ------------------------------------------------------------ */


private void append(StringBuilder buf,String s)
{
if (s==null || s.length()==0)
buf.append('-');
else
buf.append(s);
}

/**
* Writes the request and response information to the output stream.
*
Expand All @@ -101,7 +109,7 @@ public void log(Request request, int status, long written)

if (_logServer)
{
buf.append(request.getServerName());
append(buf,request.getServerName());
buf.append(' ');
}

Expand All @@ -117,10 +125,7 @@ public void log(Request request, int status, long written)
buf.append(addr);
buf.append(" - ");
Authentication authentication = request.getAuthentication();
if (authentication instanceof Authentication.User)
buf.append(((Authentication.User)authentication).getUserIdentity().getUserPrincipal().getName());
else
buf.append("-");
append(buf,(authentication instanceof Authentication.User)?((Authentication.User)authentication).getUserIdentity().getUserPrincipal().getName():null);

buf.append(" [");
if (_logDateCache != null)
Expand All @@ -129,18 +134,21 @@ public void log(Request request, int status, long written)
buf.append(request.getTimeStamp());

buf.append("] \"");
buf.append(request.getMethod());
append(buf,request.getMethod());
buf.append(' ');
buf.append(request.getHttpURI().toString());
append(buf,request.getHttpURI().toString());
buf.append(' ');
buf.append(request.getProtocol());
append(buf,request.getProtocol());
buf.append("\" ");

if (status <= 0)
status = 404;
buf.append((char)('0' + ((status / 100) % 10)));
buf.append((char)('0' + ((status / 10) % 10)));
buf.append((char)('0' + (status % 10)));
if (status >=0)
{
buf.append((char)('0' + ((status / 100) % 10)));
buf.append((char)('0' + ((status / 10) % 10)));
buf.append((char)('0' + (status % 10)));
}
else
buf.append(status);

if (written >= 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public HttpChannel(Connector connector, HttpConfiguration configuration, EndPoin
input.init(_state);
_request = new Request(this, input);
_response = new Response(this, new HttpOutput(this));
_requestLog=_connector==null?null:_connector.getServer().getRequestLog();
}

public HttpChannelState getState()
Expand Down Expand Up @@ -231,7 +232,7 @@ public void recycle()
_request.recycle();
_response.recycle();
_committedMetaData=null;
_requestLog=null;
_requestLog=_connector==null?null:_connector.getServer().getRequestLog();
_written=0;
}

Expand Down Expand Up @@ -501,12 +502,11 @@ public String toString()
public void onRequest(MetaData.Request request)
{
_requests.incrementAndGet();

_request.setTimeStamp(System.currentTimeMillis());
_request.setMetaData(request);

if (_configuration.getSendDateHeader())
_response.getHttpFields().put(_connector.getServer().getDateField());

_request.setMetaData(request);
}

public void onContent(HttpInput.Content content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.http.MetaData;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

/**
* A HttpChannel customized to be transported over the HTTP/1 protocol
*/
class HttpChannelOverHttp extends HttpChannel implements HttpParser.RequestHandler, HttpParser.ProxyHandler
{
private static final Logger LOG = Log.getLogger(HttpChannelOverHttp.class);

private final HttpConnection _httpConnection;
private final HttpFields _fields = new HttpFields();
private HttpField _connection;
Expand Down Expand Up @@ -226,6 +230,16 @@ public boolean content(ByteBuffer content)
public void badMessage(int status, String reason)
{
_httpConnection._generator.setPersistent(false);
try
{
// Need to call onRequest, so RequestLog can reports as much as possible
onRequest(_metadata);
}
catch (Exception e)
{
LOG.ignore(e);
}

onBadMessage(status,reason);
}

Expand Down
17 changes: 15 additions & 2 deletions jetty-server/src/main/java/org/eclipse/jetty/server/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,12 @@ public String getPathTranslated()
@Override
public String getProtocol()
{
return _metadata==null?null:_metadata.getVersion().toString();
if (_metadata==null)
return null;
HttpVersion version = _metadata.getVersion();
if (version==null)
return null;
return version.toString();
}

/* ------------------------------------------------------------ */
Expand Down Expand Up @@ -1636,21 +1641,29 @@ public void setMetaData(org.eclipse.jetty.http.MetaData.Request request)
uri.setPath(path);
}
else
{
setPathInfo("");
throw new BadMessageException(400,"Bad URI");
}
info=path;
}
else if (!path.startsWith("/"))
{
System.err.println(request);
if (!"*".equals(path) && !HttpMethod.CONNECT.is(getMethod()))
{
setPathInfo(path);
throw new BadMessageException(400,"Bad URI");
}
info=path;
}
else
info = URIUtil.canonicalPath(path);// TODO should this be done prior to decoding???

if (info == null)
{
setPathInfo(path);
throw new BadMessageException(400,"Bad URI");
}

setPathInfo(info);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

package org.eclipse.jetty.server;

import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.server.handler.RequestLogHandler;

/**
* A <code>RequestLog</code> can be attached to a {@link org.eclipse.jetty.server.handler.RequestLogHandler} to enable
* logging of requests/responses.
* @see RequestLogHandler#setRequestLog(RequestLog)
* @see Server#setRequestLog(RequestLog)
*/
public interface RequestLog extends LifeCycle
public interface RequestLog
{
public void log(Request request, int status, long written);
}
15 changes: 13 additions & 2 deletions jetty-server/src/main/java/org/eclipse/jetty/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class Server extends HandlerWrapper implements Attributes
private boolean _stopAtShutdown;
private boolean _dumpAfterStart=false;
private boolean _dumpBeforeStop=false;
private RequestLog _requestLog;

private volatile DateField _dateField;

Expand Down Expand Up @@ -121,8 +122,6 @@ public Server(@Name("address")InetSocketAddress addr)
setConnectors(new Connector[]{connector});
}



/* ------------------------------------------------------------ */
public Server(@Name("threadpool") ThreadPool pool)
{
Expand All @@ -131,6 +130,18 @@ public Server(@Name("threadpool") ThreadPool pool)
setServer(this);
}

/* ------------------------------------------------------------ */
public RequestLog getRequestLog()
{
return _requestLog;
}

/* ------------------------------------------------------------ */
public void setRequestLog(RequestLog requestLog)
{
updateBean(_requestLog,requestLog);
_requestLog = requestLog;
}

/* ------------------------------------------------------------ */
@ManagedAttribute("version of this server")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@
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.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;



/**
* RequestLogHandler.
* This handler can be used to wrap an individual context for context logging.
* To set a {@link RequestLog} instance for the entire {@link Server}, use
* {@link Server#setRequestLog(RequestLog)} instead of this handler.
*
*
* @see Server#setRequestLog(RequestLog)
* @org.apache.xbean.XBean
*/
public class RequestLogHandler extends HandlerWrapper
Expand Down

0 comments on commit e3bda4e

Please sign in to comment.