comparison src/org/eclipse/jetty/server/handler/RequestLogHandler.java @ 856:ea1768c00d03

remove NCSARequestLog and RequestLog
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 21 Sep 2016 00:19:26 -0600
parents 22a4e93ed20e
children 23a57aad34c0
comparison
equal deleted inserted replaced
855:fa6158f29c45 856:ea1768c00d03
16 // ======================================================================== 16 // ========================================================================
17 // 17 //
18 18
19 package org.eclipse.jetty.server.handler; 19 package org.eclipse.jetty.server.handler;
20 20
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.OutputStream;
24 import java.io.OutputStreamWriter;
25 import java.io.Writer;
21 import java.io.IOException; 26 import java.io.IOException;
27 import java.text.DateFormat;
28 import java.text.SimpleDateFormat;
29 import java.util.Locale;
30 import java.util.TimeZone;
22 31
23 import javax.servlet.DispatcherType; 32 import javax.servlet.DispatcherType;
24 import javax.servlet.ServletException; 33 import javax.servlet.ServletException;
34 import javax.servlet.http.Cookie;
25 import javax.servlet.http.HttpServletRequest; 35 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse; 36 import javax.servlet.http.HttpServletResponse;
27 37
28 import org.eclipse.jetty.continuation.Continuation; 38 import org.eclipse.jetty.continuation.Continuation;
29 import org.eclipse.jetty.continuation.ContinuationListener; 39 import org.eclipse.jetty.continuation.ContinuationListener;
40 import org.eclipse.jetty.http.HttpHeaders;
30 import org.eclipse.jetty.server.AsyncContinuation; 41 import org.eclipse.jetty.server.AsyncContinuation;
31 import org.eclipse.jetty.server.Request; 42 import org.eclipse.jetty.server.Request;
32 import org.eclipse.jetty.server.RequestLog;
33 import org.eclipse.jetty.server.Response; 43 import org.eclipse.jetty.server.Response;
34 import org.eclipse.jetty.server.Server; 44 import org.eclipse.jetty.server.Server;
35 import org.eclipse.jetty.util.component.AbstractLifeCycle; 45 import org.eclipse.jetty.util.component.AbstractLifeCycle;
36 import org.slf4j.Logger; 46 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory; 47 import org.slf4j.LoggerFactory;
43 * 53 *
44 * @org.apache.xbean.XBean 54 * @org.apache.xbean.XBean
45 */ 55 */
46 public class RequestLogHandler extends HandlerWrapper 56 public class RequestLogHandler extends HandlerWrapper
47 { 57 {
48 private static final Logger LOG = LoggerFactory.getLogger(RequestLogHandler.class); 58 private static final Logger LOG = LoggerFactory.getLogger(RequestLogHandler.class);
49 59
50 private RequestLog _requestLog; 60 /* ------------------------------------------------------------ */
51 61 /*
52 /* ------------------------------------------------------------ */ 62 * @see org.eclipse.jetty.server.server.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
53 /* 63 */
54 * @see org.eclipse.jetty.server.server.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) 64 @Override
55 */ 65 public void handle(String target, final Request baseRequest, HttpServletRequest request, final HttpServletResponse response)
56 @Override 66 throws IOException, ServletException
57 public void handle(String target, final Request baseRequest, HttpServletRequest request, final HttpServletResponse response) 67 {
58 throws IOException, ServletException 68 AsyncContinuation continuation = baseRequest.getAsyncContinuation();
59 { 69 if (!continuation.isInitial())
60 AsyncContinuation continuation = baseRequest.getAsyncContinuation(); 70 {
61 if (!continuation.isInitial()) 71 baseRequest.setDispatchTime(System.currentTimeMillis());
62 { 72 }
63 baseRequest.setDispatchTime(System.currentTimeMillis()); 73
64 } 74 try
65 75 {
66 try 76 super.handle(target, baseRequest, request, response);
67 { 77 }
68 super.handle(target, baseRequest, request, response); 78 finally
69 } 79 {
70 finally 80 if (baseRequest.getDispatcherType().equals(DispatcherType.REQUEST))
71 { 81 {
72 if (_requestLog != null && baseRequest.getDispatcherType().equals(DispatcherType.REQUEST)) 82 if (continuation.isAsync())
73 { 83 {
74 if (continuation.isAsync()) 84 if (continuation.isInitial())
75 { 85 continuation.addContinuationListener(new ContinuationListener()
76 if (continuation.isInitial()) 86 {
77 continuation.addContinuationListener(new ContinuationListener() 87
78 { 88 public void onTimeout(Continuation continuation)
79 89 {
80 public void onTimeout(Continuation continuation) 90
81 { 91 }
82 92
83 } 93 public void onComplete(Continuation continuation)
84 94 {
85 public void onComplete(Continuation continuation) 95 log(baseRequest, (Response)response);
86 { 96 }
87 _requestLog.log(baseRequest, (Response)response); 97 });
88 } 98 }
89 }); 99 else
90 } 100 log(baseRequest, (Response)response);
91 else 101 }
92 _requestLog.log(baseRequest, (Response)response); 102 }
93 } 103 }
94 } 104
95 } 105 /* ------------------------------------------------------------ */
96 106
97 /* ------------------------------------------------------------ */ 107 /* ------------------------------------------------------------ */
98 public void setRequestLog(RequestLog requestLog) 108 /*
99 { 109 * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#doStart()
100 //are we changing the request log impl? 110 */
101 try 111 @Override
102 { 112 protected void doStart() throws Exception
103 if (_requestLog != null) 113 {
104 _requestLog.stop(); 114 super.doStart();
105 } 115 startLog();
106 catch (Exception e) 116 }
107 { 117
108 LOG.warn ("",e); 118 /* ------------------------------------------------------------ */
109 } 119 /*
110 120 * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#doStop()
111 _requestLog = requestLog; 121 */
112 122 @Override
113 //if we're already started, then start our request log 123 protected void doStop() throws Exception
114 try 124 {
115 { 125 super.doStop();
116 if (isStarted() && (_requestLog != null)) 126 stopLog();
117 _requestLog.start(); 127 }
118 } 128
119 catch (Exception e) 129
120 { 130
121 throw new RuntimeException (e); 131 // from NCSARequestLog
122 } 132
123 } 133 public String filename = null;
124 134 private boolean extended = false;
125 /* ------------------------------------------------------------ */ 135 public long retainTime = 1000L*60*60*24*31; // 31 days
126 public RequestLog getRequestLog() 136 private boolean _closeOut;
127 { 137 public boolean preferProxiedForAddress = false;
128 return _requestLog; 138 public String logDateFormat = "dd/MMM/yyyy:HH:mm:ss Z";
129 } 139 public Locale logLocale = Locale.getDefault();
130 140 public TimeZone timeZone = TimeZone.getTimeZone("GMT");
131 /* ------------------------------------------------------------ */ 141 public boolean logLatency = false;
132 /* 142 public boolean logCookies = false;
133 * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#doStart() 143 public boolean logServer = false;
134 */ 144 public boolean logDispatch = false;
135 @Override 145
136 protected void doStart() throws Exception 146 private transient OutputStream _out;
137 { 147 private transient DateFormat dateFormat;
138 if (_requestLog==null) 148 private transient Writer _writer;
139 { 149
140 LOG.warn("!RequestLog"); 150 /* ------------------------------------------------------------ */
141 _requestLog=new NullRequestLog(); 151 /**
142 } 152 * Writes the request and response information to the output stream.
143 super.doStart(); 153 *
144 _requestLog.start(); 154 * @see org.eclipse.jetty.server.RequestLog#log(org.eclipse.jetty.server.Request, org.eclipse.jetty.server.Response)
145 } 155 */
146 156 public void log(Request request, Response response)
147 /* ------------------------------------------------------------ */ 157 {
148 /* 158 try
149 * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#doStop() 159 {
150 */ 160 if (_out == null)
151 @Override 161 return;
152 protected void doStop() throws Exception 162
153 { 163 StringBuilder buf = new StringBuilder(256);
154 super.doStop(); 164
155 _requestLog.stop(); 165 if (logServer)
156 if (_requestLog instanceof NullRequestLog) 166 {
157 _requestLog=null; 167 buf.append(request.getServerName());
158 } 168 buf.append(' ');
159 169 }
160 /* ------------------------------------------------------------ */ 170
161 /* ------------------------------------------------------------ */ 171 String addr = null;
162 /* ------------------------------------------------------------ */ 172 if (preferProxiedForAddress)
163 private static class NullRequestLog extends AbstractLifeCycle implements RequestLog 173 {
164 { 174 addr = request.getHeader(HttpHeaders.X_FORWARDED_FOR);
165 public void log(Request request, Response response) 175 }
166 { 176
167 } 177 if (addr == null)
168 } 178 addr = request.getRemoteAddr();
169 179
180 buf.append(addr);
181 buf.append(" - [");
182
183 synchronized(dateFormat) {
184 buf.append(dateFormat.format(request.getTimeStamp()));
185 }
186
187 buf.append("] \"");
188 buf.append(request.getMethod());
189 buf.append(' ');
190 buf.append(request.getUri().toString());
191 buf.append(' ');
192 buf.append(request.getProtocol());
193 buf.append("\" ");
194 if (request.getAsyncContinuation().isInitial())
195 {
196 int status = response.getStatus();
197 if (status <= 0)
198 status = 404;
199 buf.append((char)('0' + ((status / 100) % 10)));
200 buf.append((char)('0' + ((status / 10) % 10)));
201 buf.append((char)('0' + (status % 10)));
202 }
203 else
204 buf.append("Async");
205
206 long responseLength = response.getContentCount();
207 if (responseLength >= 0)
208 {
209 buf.append(' ');
210 if (responseLength > 99999)
211 buf.append(responseLength);
212 else
213 {
214 if (responseLength > 9999)
215 buf.append((char)('0' + ((responseLength / 10000) % 10)));
216 if (responseLength > 999)
217 buf.append((char)('0' + ((responseLength / 1000) % 10)));
218 if (responseLength > 99)
219 buf.append((char)('0' + ((responseLength / 100) % 10)));
220 if (responseLength > 9)
221 buf.append((char)('0' + ((responseLength / 10) % 10)));
222 buf.append((char)('0' + (responseLength) % 10));
223 }
224 buf.append(' ');
225 }
226 else
227 buf.append(" - ");
228
229
230 if (extended)
231 logExtended(request, response, buf);
232
233 if (logCookies)
234 {
235 Cookie[] cookies = request.getCookies();
236 if (cookies == null || cookies.length == 0)
237 buf.append(" -");
238 else
239 {
240 buf.append(" \"");
241 for (int i = 0; i < cookies.length; i++)
242 {
243 if (i != 0)
244 buf.append(';');
245 buf.append(cookies[i].getName());
246 buf.append('=');
247 buf.append(cookies[i].getValue());
248 }
249 buf.append('\"');
250 }
251 }
252
253 if (logDispatch || logLatency)
254 {
255 long now = System.currentTimeMillis();
256
257 if (logDispatch)
258 {
259 long d = request.getDispatchTime();
260 buf.append(' ');
261 buf.append(now - (d==0 ? request.getTimeStamp():d));
262 }
263
264 if (logLatency)
265 {
266 buf.append(' ');
267 buf.append(now - request.getTimeStamp());
268 }
269 }
270
271 buf.append('\n');
272
273 String log = buf.toString();
274 write(log);
275 }
276 catch (IOException e)
277 {
278 LOG.warn("",e);
279 }
280 }
281
282 /* ------------------------------------------------------------ */
283 protected synchronized void write(String log) throws IOException
284 {
285 if (_writer==null)
286 return;
287 _writer.write(log);
288 _writer.flush();
289 }
290
291
292 /* ------------------------------------------------------------ */
293 /**
294 * Writes extended request and response information to the output stream.
295 *
296 * @param request request object
297 * @param response response object
298 * @param b StringBuilder to write to
299 * @throws IOException
300 */
301 protected void logExtended(Request request,
302 Response response,
303 StringBuilder b) throws IOException
304 {
305 String referer = request.getHeader(HttpHeaders.REFERER);
306 if (referer == null)
307 b.append("\"-\" ");
308 else
309 {
310 b.append('"');
311 b.append(referer);
312 b.append("\" ");
313 }
314
315 String agent = request.getHeader(HttpHeaders.USER_AGENT);
316 if (agent == null)
317 b.append("\"-\" ");
318 else
319 {
320 b.append('"');
321 b.append(agent);
322 b.append('"');
323 }
324 }
325
326 private synchronized void startLog() throws Exception {
327 dateFormat = new SimpleDateFormat(logDateFormat,logLocale);
328 dateFormat.setTimeZone(timeZone);
329
330 if (filename != null) {
331 File file = new File(filename);
332 if( file.exists() ) {
333 File old = new File(filename+".old");
334 if( old.exists() && file.lastModified() - old.lastModified() > retainTime )
335 old.delete();
336 if( !old.exists() )
337 file.renameTo(old);
338 }
339 _out = new FileOutputStream(file,true);
340 _closeOut = true;
341 LOG.info("Opened " + filename);
342 }
343 else
344 _out = System.err;
345
346 _writer = new OutputStreamWriter(_out);
347 }
348
349 private synchronized void stopLog() throws Exception {
350 try {
351 if (_writer != null)
352 _writer.flush();
353 } catch (IOException e) {
354 LOG.trace("",e);
355 }
356 if (_closeOut) {
357 try {
358 _out.close();
359 } catch (IOException e) {
360 LOG.trace("",e);
361 }
362 }
363 _out = null;
364 _closeOut = false;
365 dateFormat = null;
366 _writer = null;
367 }
368
170 } 369 }