Mercurial Hosting > luan
changeset 856:ea1768c00d03
remove NCSARequestLog and RequestLog
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 21 Sep 2016 00:19:26 -0600 |
parents | fa6158f29c45 |
children | c20e2a4908d1 |
files | src/luan/modules/PackageLuan.java src/luan/modules/http/Server.luan src/org/eclipse/jetty/server/NCSARequestLog.java src/org/eclipse/jetty/server/RequestLog.java src/org/eclipse/jetty/server/handler/RequestLogHandler.java |
diffstat | 5 files changed, 315 insertions(+), 711 deletions(-) [+] |
line wrap: on
line diff
diff -r fa6158f29c45 -r ea1768c00d03 src/luan/modules/PackageLuan.java --- a/src/luan/modules/PackageLuan.java Tue Sep 20 21:38:44 2016 -0600 +++ b/src/luan/modules/PackageLuan.java Wed Sep 21 00:19:26 2016 -0600 @@ -45,6 +45,8 @@ if( mod == null ) { if( modName.startsWith("java:") ) { mod = JavaLuan.load(luan,modName.substring(5)); + if( mod == null ) + mod = Boolean.FALSE; } else { String src = read(luan,modName); if( src == null ) {
diff -r fa6158f29c45 -r ea1768c00d03 src/luan/modules/http/Server.luan --- a/src/luan/modules/http/Server.luan Tue Sep 20 21:38:44 2016 -0600 +++ b/src/luan/modules/http/Server.luan Wed Sep 21 00:19:26 2016 -0600 @@ -14,7 +14,6 @@ java() local Server = require "java:org.eclipse.jetty.server.Server" -local NCSARequestLog = require "java:org.eclipse.jetty.server.NCSARequestLog" local DefaultHandler = require "java:org.eclipse.jetty.server.handler.DefaultHandler" local HandlerList = require "java:org.eclipse.jetty.server.handler.HandlerList" local HandlerCollection = require "java:org.eclipse.jetty.server.handler.HandlerCollection" @@ -63,10 +62,7 @@ M.handler_wrapper.setHandler(h) end -M.log = NCSARequestLog.new() -M.log.setExtended(false) M.log_handler = RequestLogHandler.new() -M.log_handler.setRequestLog(M.log) function M.set_log_file(file_name) M.log.setFilename(file_name)
diff -r fa6158f29c45 -r ea1768c00d03 src/org/eclipse/jetty/server/NCSARequestLog.java --- a/src/org/eclipse/jetty/server/NCSARequestLog.java Tue Sep 20 21:38:44 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,563 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.server; - -import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.io.File; -import java.io.FileOutputStream; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Locale; -import java.util.TimeZone; - -import javax.servlet.http.Cookie; - -import org.eclipse.jetty.http.HttpHeaders; -import org.eclipse.jetty.util.component.AbstractLifeCycle; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * This {@link RequestLog} implementation outputs logs in the pseudo-standard - * NCSA common log format. 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. - * - * @org.apache.xbean.XBean element="ncsaLog" - */ - -/* ------------------------------------------------------------ */ -/** - */ -public class NCSARequestLog extends AbstractLifeCycle implements RequestLog -{ - private static final Logger LOG = LoggerFactory.getLogger(NCSARequestLog.class); - - private String _filename; - private boolean _extended; - public long retainTime = 1000L*60*60*24*31; // 31 days - private boolean _closeOut; - private boolean _preferProxiedForAddress; - private String _logDateFormat = "dd/MMM/yyyy:HH:mm:ss Z"; - private Locale _logLocale = Locale.getDefault(); - public TimeZone timeZone = TimeZone.getTimeZone("GMT"); - private boolean _logLatency = false; - private boolean _logCookies = false; - private boolean _logServer = false; - private boolean _logDispatch = false; - - private transient OutputStream _out; - private transient DateFormat dateFormat; - private transient Writer _writer; - - /* ------------------------------------------------------------ */ - /** - * Create request log object with default settings. - */ - public NCSARequestLog() - { - _extended = true; - } - - /* ------------------------------------------------------------ */ - /** - * Create request log object with specified output file name. - * - * @param filename the file name for the request log. - * This may be in the format expected - * by {@link RolloverFileOutputStream} - */ - public NCSARequestLog(String filename) - { - _extended = true; - setFilename(filename); - } - - /* ------------------------------------------------------------ */ - /** - * Set the output file name of the request log. - * The file name may be in the format expected by - * {@link RolloverFileOutputStream}. - * - * @param filename file name of the request log - * - */ - public void setFilename(String filename) - { - if (filename != null) - { - filename = filename.trim(); - if (filename.length() == 0) - filename = null; - } - _filename = filename; - } - - /* ------------------------------------------------------------ */ - /** - * Retrieve the output file name of the request log. - * - * @return file name of the request log - */ - public String getFilename() - { - return _filename; - } - - /* ------------------------------------------------------------ */ - /** - * Set the timestamp format for request log entries in the file. - * If this is not set, the pre-formated request timestamp is used. - * - * @param format timestamp format string - */ - public void setLogDateFormat(String format) - { - _logDateFormat = format; - } - - /* ------------------------------------------------------------ */ - /** - * Retrieve the timestamp format string for request log entries. - * - * @return timestamp format string. - */ - public String getLogDateFormat() - { - return _logDateFormat; - } - - /* ------------------------------------------------------------ */ - /** - * Set the locale of the request log. - * - * @param logLocale locale object - */ - public void setLogLocale(Locale logLocale) - { - _logLocale = logLocale; - } - - /* ------------------------------------------------------------ */ - /** - * Retrieve the locale of the request log. - * - * @return locale object - */ - public Locale getLogLocale() - { - return _logLocale; - } - - /* ------------------------------------------------------------ */ - /** - * Set the extended request log format flag. - * - * @param extended true - log the extended request information, - * false - do not log the extended request information - */ - public void setExtended(boolean extended) - { - _extended = extended; - } - - /* ------------------------------------------------------------ */ - /** - * Retrieve the extended request log format flag. - * - * @return value of the flag - */ - public boolean isExtended() - { - return _extended; - } - - /* ------------------------------------------------------------ */ - /** - * Controls logging of the request cookies. - * - * @param logCookies true - values of request cookies will be logged, - * false - values of request cookies will not be logged - */ - public void setLogCookies(boolean logCookies) - { - _logCookies = logCookies; - } - - /* ------------------------------------------------------------ */ - /** - * Retrieve log cookies flag - * - * @return value of the flag - */ - public boolean getLogCookies() - { - return _logCookies; - } - - /* ------------------------------------------------------------ */ - /** - * Controls logging of the request hostname. - * - * @param logServer true - request hostname will be logged, - * false - request hostname will not be logged - */ - public void setLogServer(boolean logServer) - { - _logServer = logServer; - } - - /* ------------------------------------------------------------ */ - /** - * Retrieve log hostname flag. - * - * @return value of the flag - */ - public boolean getLogServer() - { - return _logServer; - } - - /* ------------------------------------------------------------ */ - /** - * Controls logging of request processing time. - * - * @param logLatency true - request processing time will be logged - * false - request processing time will not be logged - */ - public void setLogLatency(boolean logLatency) - { - _logLatency = logLatency; - } - - /* ------------------------------------------------------------ */ - /** - * Retrieve log request processing time flag. - * - * @return value of the flag - */ - public boolean getLogLatency() - { - return _logLatency; - } - - /* ------------------------------------------------------------ */ - /** - * Controls whether the actual IP address of the connection or - * the IP address from the X-Forwarded-For header will be logged. - * - * @param preferProxiedForAddress true - IP address from header will be logged, - * false - IP address from the connection will be logged - */ - public void setPreferProxiedForAddress(boolean preferProxiedForAddress) - { - _preferProxiedForAddress = preferProxiedForAddress; - } - - /* ------------------------------------------------------------ */ - /** - * Retrieved log X-Forwarded-For IP address flag. - * - * @return value of the flag - */ - public boolean getPreferProxiedForAddress() - { - return _preferProxiedForAddress; - } - - /* ------------------------------------------------------------ */ - /** - * Controls logging of the request dispatch time - * - * @param value true - request dispatch time will be logged - * false - request dispatch time will not be logged - */ - public void setLogDispatch(boolean value) - { - _logDispatch = value; - } - - /* ------------------------------------------------------------ */ - /** - * Retrieve request dispatch time logging flag - * - * @return value of the flag - */ - public boolean isLogDispatch() - { - return _logDispatch; - } - - /* ------------------------------------------------------------ */ - /** - * Writes the request and response information to the output stream. - * - * @see org.eclipse.jetty.server.RequestLog#log(org.eclipse.jetty.server.Request, org.eclipse.jetty.server.Response) - */ - public void log(Request request, Response response) - { - try - { - if (_out == null) - return; - - StringBuilder buf = new StringBuilder(256); - - if (_logServer) - { - buf.append(request.getServerName()); - buf.append(' '); - } - - String addr = null; - if (_preferProxiedForAddress) - { - addr = request.getHeader(HttpHeaders.X_FORWARDED_FOR); - } - - if (addr == null) - addr = request.getRemoteAddr(); - - buf.append(addr); - buf.append(" - ["); - - synchronized(dateFormat) { - buf.append(dateFormat.format(request.getTimeStamp())); - } - - buf.append("] \""); - buf.append(request.getMethod()); - buf.append(' '); - buf.append(request.getUri().toString()); - buf.append(' '); - buf.append(request.getProtocol()); - buf.append("\" "); - if (request.getAsyncContinuation().isInitial()) - { - int status = response.getStatus(); - 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))); - } - else - buf.append("Async"); - - long responseLength = response.getContentCount(); - if (responseLength >= 0) - { - buf.append(' '); - if (responseLength > 99999) - buf.append(responseLength); - else - { - if (responseLength > 9999) - buf.append((char)('0' + ((responseLength / 10000) % 10))); - if (responseLength > 999) - buf.append((char)('0' + ((responseLength / 1000) % 10))); - if (responseLength > 99) - buf.append((char)('0' + ((responseLength / 100) % 10))); - if (responseLength > 9) - buf.append((char)('0' + ((responseLength / 10) % 10))); - buf.append((char)('0' + (responseLength) % 10)); - } - buf.append(' '); - } - else - buf.append(" - "); - - - if (_extended) - logExtended(request, response, buf); - - if (_logCookies) - { - Cookie[] cookies = request.getCookies(); - if (cookies == null || cookies.length == 0) - buf.append(" -"); - else - { - buf.append(" \""); - for (int i = 0; i < cookies.length; i++) - { - if (i != 0) - buf.append(';'); - buf.append(cookies[i].getName()); - buf.append('='); - buf.append(cookies[i].getValue()); - } - buf.append('\"'); - } - } - - if (_logDispatch || _logLatency) - { - long now = System.currentTimeMillis(); - - if (_logDispatch) - { - long d = request.getDispatchTime(); - buf.append(' '); - buf.append(now - (d==0 ? request.getTimeStamp():d)); - } - - if (_logLatency) - { - buf.append(' '); - buf.append(now - request.getTimeStamp()); - } - } - - buf.append('\n'); - - String log = buf.toString(); - write(log); - } - catch (IOException e) - { - LOG.warn("",e); - } - } - - /* ------------------------------------------------------------ */ - protected void write(String log) throws IOException - { - synchronized(this) - { - if (_writer==null) - return; - _writer.write(log); - _writer.flush(); - } - } - - - /* ------------------------------------------------------------ */ - /** - * Writes extended request and response information to the output stream. - * - * @param request request object - * @param response response object - * @param b StringBuilder to write to - * @throws IOException - */ - protected void logExtended(Request request, - Response response, - StringBuilder b) throws IOException - { - String referer = request.getHeader(HttpHeaders.REFERER); - if (referer == null) - b.append("\"-\" "); - else - { - b.append('"'); - b.append(referer); - b.append("\" "); - } - - String agent = request.getHeader(HttpHeaders.USER_AGENT); - if (agent == null) - b.append("\"-\" "); - else - { - b.append('"'); - b.append(agent); - b.append('"'); - } - } - - /* ------------------------------------------------------------ */ - /** - * Set up request logging and open log file. - * - * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart() - */ - @Override - protected synchronized void doStart() throws Exception - { - dateFormat = new SimpleDateFormat(_logDateFormat,_logLocale); - dateFormat.setTimeZone(timeZone); - - if (_filename != null) { - File file = new File(_filename); - if( file.exists() ) { - File old = new File(_filename+".old"); - if( old.exists() && file.lastModified() - old.lastModified() > retainTime ) - old.delete(); - if( !old.exists() ) - file.renameTo(old); - } - _out = new FileOutputStream(file,true); - _closeOut = true; - LOG.info("Opened " + _filename); - } - else - _out = System.err; - - synchronized(this) - { - _writer = new OutputStreamWriter(_out); - } - super.doStart(); - } - - /* ------------------------------------------------------------ */ - /** - * Close the log file and perform cleanup. - * - * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStop() - */ - @Override - protected void doStop() throws Exception - { - synchronized (this) - { - super.doStop(); - try - { - if (_writer != null) - _writer.flush(); - } - catch (IOException e) - { - LOG.trace("",e); - } - if (_closeOut) - try - { - _out.close(); - } - catch (IOException e) - { - LOG.trace("",e); - } - - _out = null; - _closeOut = false; - dateFormat = null; - _writer = null; - } - } -}
diff -r fa6158f29c45 -r ea1768c00d03 src/org/eclipse/jetty/server/RequestLog.java --- a/src/org/eclipse/jetty/server/RequestLog.java Tue Sep 20 21:38:44 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.server; - -import org.eclipse.jetty.util.component.LifeCycle; - -/** - * A <code>RequestLog</code> can be attached to a {@link org.eclipse.jetty.server.handler.RequestLogHandler} to enable - * logging of requests/responses. - */ -public interface RequestLog extends LifeCycle -{ - public void log(Request request, Response response); -}
diff -r fa6158f29c45 -r ea1768c00d03 src/org/eclipse/jetty/server/handler/RequestLogHandler.java --- a/src/org/eclipse/jetty/server/handler/RequestLogHandler.java Tue Sep 20 21:38:44 2016 -0600 +++ b/src/org/eclipse/jetty/server/handler/RequestLogHandler.java Wed Sep 21 00:19:26 2016 -0600 @@ -18,18 +18,28 @@ package org.eclipse.jetty.server.handler; +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; import java.io.IOException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Locale; +import java.util.TimeZone; import javax.servlet.DispatcherType; import javax.servlet.ServletException; +import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.continuation.Continuation; import org.eclipse.jetty.continuation.ContinuationListener; +import org.eclipse.jetty.http.HttpHeaders; import org.eclipse.jetty.server.AsyncContinuation; 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; @@ -45,126 +55,315 @@ */ public class RequestLogHandler extends HandlerWrapper { - private static final Logger LOG = LoggerFactory.getLogger(RequestLogHandler.class); + private static final Logger LOG = LoggerFactory.getLogger(RequestLogHandler.class); + + /* ------------------------------------------------------------ */ + /* + * @see org.eclipse.jetty.server.server.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) + */ + @Override + public void handle(String target, final Request baseRequest, HttpServletRequest request, final HttpServletResponse response) + throws IOException, ServletException + { + AsyncContinuation continuation = baseRequest.getAsyncContinuation(); + if (!continuation.isInitial()) + { + baseRequest.setDispatchTime(System.currentTimeMillis()); + } + + try + { + super.handle(target, baseRequest, request, response); + } + finally + { + if (baseRequest.getDispatcherType().equals(DispatcherType.REQUEST)) + { + if (continuation.isAsync()) + { + if (continuation.isInitial()) + continuation.addContinuationListener(new ContinuationListener() + { + + public void onTimeout(Continuation continuation) + { + + } + + public void onComplete(Continuation continuation) + { + log(baseRequest, (Response)response); + } + }); + } + else + log(baseRequest, (Response)response); + } + } + } + + /* ------------------------------------------------------------ */ + + /* ------------------------------------------------------------ */ + /* + * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#doStart() + */ + @Override + protected void doStart() throws Exception + { + super.doStart(); + startLog(); + } + + /* ------------------------------------------------------------ */ + /* + * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#doStop() + */ + @Override + protected void doStop() throws Exception + { + super.doStop(); + stopLog(); + } + + - private RequestLog _requestLog; - - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.jetty.server.server.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) - */ - @Override - public void handle(String target, final Request baseRequest, HttpServletRequest request, final HttpServletResponse response) - throws IOException, ServletException - { - AsyncContinuation continuation = baseRequest.getAsyncContinuation(); - if (!continuation.isInitial()) - { - baseRequest.setDispatchTime(System.currentTimeMillis()); - } - - try - { - super.handle(target, baseRequest, request, response); - } - finally - { - if (_requestLog != null && baseRequest.getDispatcherType().equals(DispatcherType.REQUEST)) - { - if (continuation.isAsync()) - { - if (continuation.isInitial()) - continuation.addContinuationListener(new ContinuationListener() - { + // from NCSARequestLog + + public String filename = null; + private boolean extended = false; + public long retainTime = 1000L*60*60*24*31; // 31 days + private boolean _closeOut; + public boolean preferProxiedForAddress = false; + public String logDateFormat = "dd/MMM/yyyy:HH:mm:ss Z"; + public Locale logLocale = Locale.getDefault(); + public TimeZone timeZone = TimeZone.getTimeZone("GMT"); + public boolean logLatency = false; + public boolean logCookies = false; + public boolean logServer = false; + public boolean logDispatch = false; + + private transient OutputStream _out; + private transient DateFormat dateFormat; + private transient Writer _writer; + + /* ------------------------------------------------------------ */ + /** + * Writes the request and response information to the output stream. + * + * @see org.eclipse.jetty.server.RequestLog#log(org.eclipse.jetty.server.Request, org.eclipse.jetty.server.Response) + */ + public void log(Request request, Response response) + { + try + { + if (_out == null) + return; + + StringBuilder buf = new StringBuilder(256); - public void onTimeout(Continuation continuation) - { + if (logServer) + { + buf.append(request.getServerName()); + buf.append(' '); + } - } + String addr = null; + if (preferProxiedForAddress) + { + addr = request.getHeader(HttpHeaders.X_FORWARDED_FOR); + } + + if (addr == null) + addr = request.getRemoteAddr(); + + buf.append(addr); + buf.append(" - ["); - public void onComplete(Continuation continuation) - { - _requestLog.log(baseRequest, (Response)response); - } - }); - } - else - _requestLog.log(baseRequest, (Response)response); - } - } - } + synchronized(dateFormat) { + buf.append(dateFormat.format(request.getTimeStamp())); + } + + buf.append("] \""); + buf.append(request.getMethod()); + buf.append(' '); + buf.append(request.getUri().toString()); + buf.append(' '); + buf.append(request.getProtocol()); + buf.append("\" "); + if (request.getAsyncContinuation().isInitial()) + { + int status = response.getStatus(); + 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))); + } + else + buf.append("Async"); - /* ------------------------------------------------------------ */ - public void setRequestLog(RequestLog requestLog) - { - //are we changing the request log impl? - try - { - if (_requestLog != null) - _requestLog.stop(); - } - catch (Exception e) - { - LOG.warn ("",e); - } - - _requestLog = requestLog; - - //if we're already started, then start our request log - try - { - if (isStarted() && (_requestLog != null)) - _requestLog.start(); - } - catch (Exception e) - { - throw new RuntimeException (e); - } - } + long responseLength = response.getContentCount(); + if (responseLength >= 0) + { + buf.append(' '); + if (responseLength > 99999) + buf.append(responseLength); + else + { + if (responseLength > 9999) + buf.append((char)('0' + ((responseLength / 10000) % 10))); + if (responseLength > 999) + buf.append((char)('0' + ((responseLength / 1000) % 10))); + if (responseLength > 99) + buf.append((char)('0' + ((responseLength / 100) % 10))); + if (responseLength > 9) + buf.append((char)('0' + ((responseLength / 10) % 10))); + buf.append((char)('0' + (responseLength) % 10)); + } + buf.append(' '); + } + else + buf.append(" - "); + + + if (extended) + logExtended(request, response, buf); - /* ------------------------------------------------------------ */ - public RequestLog getRequestLog() - { - return _requestLog; - } + if (logCookies) + { + Cookie[] cookies = request.getCookies(); + if (cookies == null || cookies.length == 0) + buf.append(" -"); + else + { + buf.append(" \""); + for (int i = 0; i < cookies.length; i++) + { + if (i != 0) + buf.append(';'); + buf.append(cookies[i].getName()); + buf.append('='); + buf.append(cookies[i].getValue()); + } + buf.append('\"'); + } + } + + if (logDispatch || logLatency) + { + long now = System.currentTimeMillis(); + + if (logDispatch) + { + long d = request.getDispatchTime(); + buf.append(' '); + buf.append(now - (d==0 ? request.getTimeStamp():d)); + } + + if (logLatency) + { + buf.append(' '); + buf.append(now - request.getTimeStamp()); + } + } + + buf.append('\n'); + + String log = buf.toString(); + write(log); + } + catch (IOException e) + { + LOG.warn("",e); + } + } - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#doStart() - */ - @Override - protected void doStart() throws Exception - { - if (_requestLog==null) - { - LOG.warn("!RequestLog"); - _requestLog=new NullRequestLog(); - } - super.doStart(); - _requestLog.start(); - } + /* ------------------------------------------------------------ */ + protected synchronized void write(String log) throws IOException + { + if (_writer==null) + return; + _writer.write(log); + _writer.flush(); + } + + + /* ------------------------------------------------------------ */ + /** + * Writes extended request and response information to the output stream. + * + * @param request request object + * @param response response object + * @param b StringBuilder to write to + * @throws IOException + */ + protected void logExtended(Request request, + Response response, + StringBuilder b) throws IOException + { + String referer = request.getHeader(HttpHeaders.REFERER); + if (referer == null) + b.append("\"-\" "); + else + { + b.append('"'); + b.append(referer); + b.append("\" "); + } + + String agent = request.getHeader(HttpHeaders.USER_AGENT); + if (agent == null) + b.append("\"-\" "); + else + { + b.append('"'); + b.append(agent); + b.append('"'); + } + } - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#doStop() - */ - @Override - protected void doStop() throws Exception - { - super.doStop(); - _requestLog.stop(); - if (_requestLog instanceof NullRequestLog) - _requestLog=null; - } + private synchronized void startLog() throws Exception { + dateFormat = new SimpleDateFormat(logDateFormat,logLocale); + dateFormat.setTimeZone(timeZone); + + if (filename != null) { + File file = new File(filename); + if( file.exists() ) { + File old = new File(filename+".old"); + if( old.exists() && file.lastModified() - old.lastModified() > retainTime ) + old.delete(); + if( !old.exists() ) + file.renameTo(old); + } + _out = new FileOutputStream(file,true); + _closeOut = true; + LOG.info("Opened " + filename); + } + else + _out = System.err; - /* ------------------------------------------------------------ */ - /* ------------------------------------------------------------ */ - /* ------------------------------------------------------------ */ - private static class NullRequestLog extends AbstractLifeCycle implements RequestLog - { - public void log(Request request, Response response) - { - } - } - + _writer = new OutputStreamWriter(_out); + } + + private synchronized void stopLog() throws Exception { + try { + if (_writer != null) + _writer.flush(); + } catch (IOException e) { + LOG.trace("",e); + } + if (_closeOut) { + try { + _out.close(); + } catch (IOException e) { + LOG.trace("",e); + } + } + _out = null; + _closeOut = false; + dateFormat = null; + _writer = null; + } + }