Skip to content

Commit

Permalink
Issue jetty#113 - Added Request Log Writers
Browse files Browse the repository at this point in the history
Introduced the RequestLog.Writer where a RequestLog takes a writer
which manages what to do with the log strings produced by the RequestLog

deprecated the NCSA and SLF4J RequestLogs in favor of CustomRequestLog

Implemented more format codes in CustomRequestLog

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Nov 19, 2018
1 parent 7dd3cff commit e4d30b3
Show file tree
Hide file tree
Showing 9 changed files with 623 additions and 395 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@

import java.io.IOException;
import java.util.Locale;

import javax.servlet.http.Cookie;

import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.pathmap.PathMappings;
import org.eclipse.jetty.server.handler.StatisticsHandler;
import org.eclipse.jetty.util.DateCache;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

Expand All @@ -37,20 +36,16 @@
* Configuration options allow a choice between the standard Common Log Format (as used in the 3 log format) and the
* Combined Log Format (single log format). This log format can be output by most web servers, and almost all web log
* analysis software can understand these formats.
* @deprecated use {@link CustomRequestLog} given format string {@link CustomRequestLog#NCSA_FORMAT} with a {@link RequestLog.Writer}
*/
public abstract class AbstractNCSARequestLog extends AbstractLifeCycle implements RequestLog
@Deprecated
public abstract class AbstractNCSARequestLog extends ContainerLifeCycle implements RequestLog
{
protected static final Logger LOG = Log.getLogger(AbstractNCSARequestLog.class);

private static ThreadLocal<StringBuilder> _buffers = new ThreadLocal<StringBuilder>()
{
@Override
protected StringBuilder initialValue()
{
return new StringBuilder(256);
}
};
private static ThreadLocal<StringBuilder> _buffers = ThreadLocal.withInitial(() -> new StringBuilder(256));

protected final RequestLog.Writer _requestLogWriter;

private String[] _ignorePaths;
private boolean _extended;
Expand All @@ -64,24 +59,27 @@ protected StringBuilder initialValue()
private Locale _logLocale = Locale.getDefault();
private String _logTimeZone = "GMT";

/* ------------------------------------------------------------ */
protected AbstractNCSARequestLog(RequestLog.Writer requestLogWriter)
{
this._requestLogWriter = requestLogWriter;
addBean(_requestLogWriter);
}

/**
* Is logging enabled
* @return true if logging is enabled
*/
protected abstract boolean isEnabled();

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

/**
* Write requestEntry out. (to disk or slf4j log)
* @param requestEntry the request entry
* @throws IOException if unable to write the entry
*/
public abstract void write(String requestEntry) throws IOException;

/* ------------------------------------------------------------ */
public void write(String requestEntry) throws IOException
{
_requestLogWriter.write(requestEntry);
}

private void append(StringBuilder buf,String s)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,112 +18,22 @@

package org.eclipse.jetty.server;

import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

import org.eclipse.jetty.util.BlockingArrayQueue;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;


/* ------------------------------------------------------------ */
/**
* An asynchronously writing NCSA Request Log
* @deprecated use {@link CustomRequestLog} given format string {@link CustomRequestLog#NCSA_FORMAT} with an {@link AsyncRequestLogWriter}
*/
@Deprecated
public class AsyncNCSARequestLog extends NCSARequestLog
{
private static final Logger LOG = Log.getLogger(AsyncNCSARequestLog.class);
private final BlockingQueue<String> _queue;
private transient WriterThread _thread;
private boolean _warnedFull;

public AsyncNCSARequestLog()
{
this(null,null);
}

public AsyncNCSARequestLog(BlockingQueue<String> queue)
{
this(null,queue);
}

public AsyncNCSARequestLog(String filename)
{
this(filename,null);
}

public AsyncNCSARequestLog(String filename,BlockingQueue<String> queue)
{
super(filename);
if (queue==null)
queue=new BlockingArrayQueue<>(1024);
_queue=queue;
}

private class WriterThread extends Thread
public AsyncNCSARequestLog(String filename, BlockingQueue<String> queue)
{
WriterThread()
{
setName("AsyncNCSARequestLog@"+Integer.toString(AsyncNCSARequestLog.this.hashCode(),16));
}

@Override
public void run()
{
while (isRunning())
{
try
{
String log = _queue.poll(10,TimeUnit.SECONDS);
if (log!=null)
AsyncNCSARequestLog.super.write(log);

while(!_queue.isEmpty())
{
log=_queue.poll();
if (log!=null)
AsyncNCSARequestLog.super.write(log);
}
}
catch (IOException e)
{
LOG.warn(e);
}
catch (InterruptedException e)
{
LOG.ignore(e);
}
}
}
}

@Override
protected synchronized void doStart() throws Exception
{
super.doStart();
_thread = new WriterThread();
_thread.start();
}

@Override
protected void doStop() throws Exception
{
_thread.interrupt();
_thread.join();
super.doStop();
_thread=null;
}

@Override
public void write(String log) throws IOException
{
if (!_queue.offer(log))
{
if (_warnedFull)
LOG.warn("Log Queue overflow");
_warnedFull=true;
}
super(new AsyncRequestLogWriter(filename, queue));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.eclipse.jetty.server;

import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

import org.eclipse.jetty.util.BlockingArrayQueue;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

public class AsyncRequestLogWriter extends RequestLogWriter
{
private static final Logger LOG = Log.getLogger(AsyncRequestLogWriter.class);
private final BlockingQueue<String> _queue;
private transient AsyncRequestLogWriter.WriterThread _thread;
private boolean _warnedFull;

public AsyncRequestLogWriter()
{
this(null, null);
}

public AsyncRequestLogWriter(String filename,BlockingQueue<String> queue)
{
super(filename);
if (queue==null)
queue=new BlockingArrayQueue<>(1024);
_queue=queue;
}

private class WriterThread extends Thread
{
WriterThread()
{
setName("AsyncRequestLogWriter@"+Integer.toString(AsyncRequestLogWriter.this.hashCode(),16));
}

@Override
public void run()
{
while (isRunning())
{
try
{
String log = _queue.poll(10, TimeUnit.SECONDS);
if (log!=null)
AsyncRequestLogWriter.super.write(log);

while(!_queue.isEmpty())
{
log=_queue.poll();
if (log!=null)
AsyncRequestLogWriter.super.write(log);
}
}
catch (IOException e)
{
LOG.warn(e);
}
catch (InterruptedException e)
{
LOG.ignore(e);
}
}
}
}

@Override
protected synchronized void doStart() throws Exception
{
super.doStart();
_thread = new AsyncRequestLogWriter.WriterThread();
_thread.start();
}

@Override
protected void doStop() throws Exception
{
_thread.interrupt();
_thread.join();
super.doStop();
_thread=null;
}

@Override
public void write(String log) throws IOException
{
if (!_queue.offer(log))
{
if (_warnedFull)
LOG.warn("Log Queue overflow");
_warnedFull=true;
}
}
}
Loading

0 comments on commit e4d30b3

Please sign in to comment.