Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/NCSARequestLog.java @ 848:22a4e93ed20e
remove Container
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 19 Sep 2016 16:38:36 -0600 |
parents | 175577dab6d8 |
children | 3242aff51053 |
rev | line source |
---|---|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.server; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.io.IOException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import java.io.OutputStream; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import java.io.OutputStreamWriter; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 import java.io.Writer; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 import java.util.Locale; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 import java.util.TimeZone; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 import javax.servlet.http.Cookie; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 import org.eclipse.jetty.http.HttpHeaders; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 import org.eclipse.jetty.util.DateCache; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 import org.eclipse.jetty.util.RolloverFileOutputStream; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 import org.eclipse.jetty.util.StringUtil; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 import org.eclipse.jetty.util.component.AbstractLifeCycle; |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
815
diff
changeset
|
35 import org.slf4j.Logger; |
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
815
diff
changeset
|
36 import org.slf4j.LoggerFactory; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 /** |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 * This {@link RequestLog} implementation outputs logs in the pseudo-standard |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 * NCSA common log format. Configuration options allow a choice between the |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 * standard Common Log Format (as used in the 3 log format) and the Combined Log |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 * Format (single log format). This log format can be output by most web |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 * servers, and almost all web log analysis software can understand these |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 * formats. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 * @org.apache.xbean.XBean element="ncsaLog" |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
47 */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 /* ------------------------------------------------------------ */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
50 /** |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
51 */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
52 public class NCSARequestLog extends AbstractLifeCycle implements RequestLog |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
53 { |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
815
diff
changeset
|
54 private static final Logger LOG = LoggerFactory.getLogger(NCSARequestLog.class); |
813 | 55 private static ThreadLocal<StringBuilder> _buffers = new ThreadLocal<StringBuilder>() |
56 { | |
57 @Override | |
58 protected StringBuilder initialValue() | |
59 { | |
60 return new StringBuilder(256); | |
61 } | |
62 }; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
63 |
813 | 64 private String _filename; |
65 private boolean _extended; | |
66 private boolean _append; | |
67 private int _retainDays; | |
68 private boolean _closeOut; | |
69 private boolean _preferProxiedForAddress; | |
70 private String _logDateFormat = "dd/MMM/yyyy:HH:mm:ss Z"; | |
71 private String _filenameDateFormat = null; | |
72 private Locale _logLocale = Locale.getDefault(); | |
73 private String _logTimeZone = "GMT"; | |
74 private boolean _logLatency = false; | |
75 private boolean _logCookies = false; | |
76 private boolean _logServer = false; | |
77 private boolean _logDispatch = false; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
78 |
813 | 79 private transient OutputStream _out; |
80 private transient OutputStream _fileOut; | |
81 private transient DateCache _logDateCache; | |
82 private transient Writer _writer; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
83 |
813 | 84 /* ------------------------------------------------------------ */ |
85 /** | |
86 * Create request log object with default settings. | |
87 */ | |
88 public NCSARequestLog() | |
89 { | |
90 _extended = true; | |
91 _append = true; | |
92 _retainDays = 31; | |
93 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
94 |
813 | 95 /* ------------------------------------------------------------ */ |
96 /** | |
97 * Create request log object with specified output file name. | |
98 * | |
99 * @param filename the file name for the request log. | |
100 * This may be in the format expected | |
101 * by {@link RolloverFileOutputStream} | |
102 */ | |
103 public NCSARequestLog(String filename) | |
104 { | |
105 _extended = true; | |
106 _append = true; | |
107 _retainDays = 31; | |
108 setFilename(filename); | |
109 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
110 |
813 | 111 /* ------------------------------------------------------------ */ |
112 /** | |
113 * Set the output file name of the request log. | |
114 * The file name may be in the format expected by | |
115 * {@link RolloverFileOutputStream}. | |
116 * | |
117 * @param filename file name of the request log | |
118 * | |
119 */ | |
120 public void setFilename(String filename) | |
121 { | |
122 if (filename != null) | |
123 { | |
124 filename = filename.trim(); | |
125 if (filename.length() == 0) | |
126 filename = null; | |
127 } | |
128 _filename = filename; | |
129 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
130 |
813 | 131 /* ------------------------------------------------------------ */ |
132 /** | |
133 * Retrieve the output file name of the request log. | |
134 * | |
135 * @return file name of the request log | |
136 */ | |
137 public String getFilename() | |
138 { | |
139 return _filename; | |
140 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
141 |
813 | 142 /* ------------------------------------------------------------ */ |
143 /** | |
144 * Retrieve the file name of the request log with the expanded | |
145 * date wildcard if the output is written to the disk using | |
146 * {@link RolloverFileOutputStream}. | |
147 * | |
148 * @return file name of the request log, or null if not applicable | |
149 */ | |
150 public String getDatedFilename() | |
151 { | |
152 if (_fileOut instanceof RolloverFileOutputStream) | |
153 return ((RolloverFileOutputStream)_fileOut).getDatedFilename(); | |
154 return null; | |
155 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
156 |
813 | 157 /* ------------------------------------------------------------ */ |
158 /** | |
159 * Set the timestamp format for request log entries in the file. | |
160 * If this is not set, the pre-formated request timestamp is used. | |
161 * | |
162 * @param format timestamp format string | |
163 */ | |
164 public void setLogDateFormat(String format) | |
165 { | |
166 _logDateFormat = format; | |
167 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
168 |
813 | 169 /* ------------------------------------------------------------ */ |
170 /** | |
171 * Retrieve the timestamp format string for request log entries. | |
172 * | |
173 * @return timestamp format string. | |
174 */ | |
175 public String getLogDateFormat() | |
176 { | |
177 return _logDateFormat; | |
178 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
179 |
813 | 180 /* ------------------------------------------------------------ */ |
181 /** | |
182 * Set the locale of the request log. | |
183 * | |
184 * @param logLocale locale object | |
185 */ | |
186 public void setLogLocale(Locale logLocale) | |
187 { | |
188 _logLocale = logLocale; | |
189 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
190 |
813 | 191 /* ------------------------------------------------------------ */ |
192 /** | |
193 * Retrieve the locale of the request log. | |
194 * | |
195 * @return locale object | |
196 */ | |
197 public Locale getLogLocale() | |
198 { | |
199 return _logLocale; | |
200 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
201 |
813 | 202 /* ------------------------------------------------------------ */ |
203 /** | |
204 * Set the timezone of the request log. | |
205 * | |
206 * @param tz timezone string | |
207 */ | |
208 public void setLogTimeZone(String tz) | |
209 { | |
210 _logTimeZone = tz; | |
211 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
212 |
813 | 213 /* ------------------------------------------------------------ */ |
214 /** | |
215 * Retrieve the timezone of the request log. | |
216 * | |
217 * @return timezone string | |
218 */ | |
219 public String getLogTimeZone() | |
220 { | |
221 return _logTimeZone; | |
222 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
223 |
813 | 224 /* ------------------------------------------------------------ */ |
225 /** | |
226 * Set the number of days before rotated log files are deleted. | |
227 * | |
228 * @param retainDays number of days to keep a log file | |
229 */ | |
230 public void setRetainDays(int retainDays) | |
231 { | |
232 _retainDays = retainDays; | |
233 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
234 |
813 | 235 /* ------------------------------------------------------------ */ |
236 /** | |
237 * Retrieve the number of days before rotated log files are deleted. | |
238 * | |
239 * @return number of days to keep a log file | |
240 */ | |
241 public int getRetainDays() | |
242 { | |
243 return _retainDays; | |
244 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
245 |
813 | 246 /* ------------------------------------------------------------ */ |
247 /** | |
248 * Set the extended request log format flag. | |
249 * | |
250 * @param extended true - log the extended request information, | |
251 * false - do not log the extended request information | |
252 */ | |
253 public void setExtended(boolean extended) | |
254 { | |
255 _extended = extended; | |
256 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
257 |
813 | 258 /* ------------------------------------------------------------ */ |
259 /** | |
260 * Retrieve the extended request log format flag. | |
261 * | |
262 * @return value of the flag | |
263 */ | |
264 public boolean isExtended() | |
265 { | |
266 return _extended; | |
267 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
268 |
813 | 269 /* ------------------------------------------------------------ */ |
270 /** | |
271 * Set append to log flag. | |
272 * | |
273 * @param append true - request log file will be appended after restart, | |
274 * false - request log file will be overwritten after restart | |
275 */ | |
276 public void setAppend(boolean append) | |
277 { | |
278 _append = append; | |
279 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
280 |
813 | 281 /* ------------------------------------------------------------ */ |
282 /** | |
283 * Retrieve append to log flag. | |
284 * | |
285 * @return value of the flag | |
286 */ | |
287 public boolean isAppend() | |
288 { | |
289 return _append; | |
290 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
291 |
813 | 292 /* ------------------------------------------------------------ */ |
293 /** | |
294 * Controls logging of the request cookies. | |
295 * | |
296 * @param logCookies true - values of request cookies will be logged, | |
297 * false - values of request cookies will not be logged | |
298 */ | |
299 public void setLogCookies(boolean logCookies) | |
300 { | |
301 _logCookies = logCookies; | |
302 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
303 |
813 | 304 /* ------------------------------------------------------------ */ |
305 /** | |
306 * Retrieve log cookies flag | |
307 * | |
308 * @return value of the flag | |
309 */ | |
310 public boolean getLogCookies() | |
311 { | |
312 return _logCookies; | |
313 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
314 |
813 | 315 /* ------------------------------------------------------------ */ |
316 /** | |
317 * Controls logging of the request hostname. | |
318 * | |
319 * @param logServer true - request hostname will be logged, | |
320 * false - request hostname will not be logged | |
321 */ | |
322 public void setLogServer(boolean logServer) | |
323 { | |
324 _logServer = logServer; | |
325 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
326 |
813 | 327 /* ------------------------------------------------------------ */ |
328 /** | |
329 * Retrieve log hostname flag. | |
330 * | |
331 * @return value of the flag | |
332 */ | |
333 public boolean getLogServer() | |
334 { | |
335 return _logServer; | |
336 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
337 |
813 | 338 /* ------------------------------------------------------------ */ |
339 /** | |
340 * Controls logging of request processing time. | |
341 * | |
342 * @param logLatency true - request processing time will be logged | |
343 * false - request processing time will not be logged | |
344 */ | |
345 public void setLogLatency(boolean logLatency) | |
346 { | |
347 _logLatency = logLatency; | |
348 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
349 |
813 | 350 /* ------------------------------------------------------------ */ |
351 /** | |
352 * Retrieve log request processing time flag. | |
353 * | |
354 * @return value of the flag | |
355 */ | |
356 public boolean getLogLatency() | |
357 { | |
358 return _logLatency; | |
359 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
360 |
813 | 361 /* ------------------------------------------------------------ */ |
362 /** | |
363 * Controls whether the actual IP address of the connection or | |
364 * the IP address from the X-Forwarded-For header will be logged. | |
365 * | |
366 * @param preferProxiedForAddress true - IP address from header will be logged, | |
367 * false - IP address from the connection will be logged | |
368 */ | |
369 public void setPreferProxiedForAddress(boolean preferProxiedForAddress) | |
370 { | |
371 _preferProxiedForAddress = preferProxiedForAddress; | |
372 } | |
373 | |
374 /* ------------------------------------------------------------ */ | |
375 /** | |
376 * Retrieved log X-Forwarded-For IP address flag. | |
377 * | |
378 * @return value of the flag | |
379 */ | |
380 public boolean getPreferProxiedForAddress() | |
381 { | |
382 return _preferProxiedForAddress; | |
383 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
384 |
813 | 385 /* ------------------------------------------------------------ */ |
386 /** | |
387 * Set the log file name date format. | |
388 * @see RolloverFileOutputStream#RolloverFileOutputStream(String, boolean, int, TimeZone, String, String) | |
389 * | |
390 * @param logFileDateFormat format string that is passed to {@link RolloverFileOutputStream} | |
391 */ | |
392 public void setFilenameDateFormat(String logFileDateFormat) | |
393 { | |
394 _filenameDateFormat = logFileDateFormat; | |
395 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
396 |
813 | 397 /* ------------------------------------------------------------ */ |
398 /** | |
399 * Retrieve the file name date format string. | |
400 * | |
401 * @return the log File Date Format | |
402 */ | |
403 public String getFilenameDateFormat() | |
404 { | |
405 return _filenameDateFormat; | |
406 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
407 |
813 | 408 /* ------------------------------------------------------------ */ |
409 /** | |
410 * Controls logging of the request dispatch time | |
411 * | |
412 * @param value true - request dispatch time will be logged | |
413 * false - request dispatch time will not be logged | |
414 */ | |
415 public void setLogDispatch(boolean value) | |
416 { | |
417 _logDispatch = value; | |
418 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
419 |
813 | 420 /* ------------------------------------------------------------ */ |
421 /** | |
422 * Retrieve request dispatch time logging flag | |
423 * | |
424 * @return value of the flag | |
425 */ | |
426 public boolean isLogDispatch() | |
427 { | |
428 return _logDispatch; | |
429 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
430 |
813 | 431 /* ------------------------------------------------------------ */ |
432 /** | |
433 * Writes the request and response information to the output stream. | |
434 * | |
435 * @see org.eclipse.jetty.server.RequestLog#log(org.eclipse.jetty.server.Request, org.eclipse.jetty.server.Response) | |
436 */ | |
437 public void log(Request request, Response response) | |
438 { | |
439 try | |
440 { | |
441 if (_fileOut == null) | |
442 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
443 |
813 | 444 StringBuilder buf= _buffers.get(); |
445 buf.setLength(0); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
446 |
813 | 447 if (_logServer) |
448 { | |
449 buf.append(request.getServerName()); | |
450 buf.append(' '); | |
451 } | |
452 | |
453 String addr = null; | |
454 if (_preferProxiedForAddress) | |
455 { | |
456 addr = request.getHeader(HttpHeaders.X_FORWARDED_FOR); | |
457 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
458 |
813 | 459 if (addr == null) |
460 addr = request.getRemoteAddr(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
461 |
813 | 462 buf.append(addr); |
463 buf.append(" - "); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
464 |
813 | 465 buf.append(" ["); |
466 if (_logDateCache != null) | |
467 buf.append(_logDateCache.format(request.getTimeStamp())); | |
468 else | |
469 buf.append(request.getTimeStampBuffer().toString()); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
470 |
813 | 471 buf.append("] \""); |
472 buf.append(request.getMethod()); | |
473 buf.append(' '); | |
474 buf.append(request.getUri().toString()); | |
475 buf.append(' '); | |
476 buf.append(request.getProtocol()); | |
477 buf.append("\" "); | |
478 if (request.getAsyncContinuation().isInitial()) | |
479 { | |
480 int status = response.getStatus(); | |
481 if (status <= 0) | |
482 status = 404; | |
483 buf.append((char)('0' + ((status / 100) % 10))); | |
484 buf.append((char)('0' + ((status / 10) % 10))); | |
485 buf.append((char)('0' + (status % 10))); | |
486 } | |
487 else | |
488 buf.append("Async"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
489 |
813 | 490 long responseLength = response.getContentCount(); |
491 if (responseLength >= 0) | |
492 { | |
493 buf.append(' '); | |
494 if (responseLength > 99999) | |
495 buf.append(responseLength); | |
496 else | |
497 { | |
498 if (responseLength > 9999) | |
499 buf.append((char)('0' + ((responseLength / 10000) % 10))); | |
500 if (responseLength > 999) | |
501 buf.append((char)('0' + ((responseLength / 1000) % 10))); | |
502 if (responseLength > 99) | |
503 buf.append((char)('0' + ((responseLength / 100) % 10))); | |
504 if (responseLength > 9) | |
505 buf.append((char)('0' + ((responseLength / 10) % 10))); | |
506 buf.append((char)('0' + (responseLength) % 10)); | |
507 } | |
508 buf.append(' '); | |
509 } | |
510 else | |
511 buf.append(" - "); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
512 |
813 | 513 |
514 if (_extended) | |
515 logExtended(request, response, buf); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
516 |
813 | 517 if (_logCookies) |
518 { | |
519 Cookie[] cookies = request.getCookies(); | |
520 if (cookies == null || cookies.length == 0) | |
521 buf.append(" -"); | |
522 else | |
523 { | |
524 buf.append(" \""); | |
525 for (int i = 0; i < cookies.length; i++) | |
526 { | |
527 if (i != 0) | |
528 buf.append(';'); | |
529 buf.append(cookies[i].getName()); | |
530 buf.append('='); | |
531 buf.append(cookies[i].getValue()); | |
532 } | |
533 buf.append('\"'); | |
534 } | |
535 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
536 |
813 | 537 if (_logDispatch || _logLatency) |
538 { | |
539 long now = System.currentTimeMillis(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
540 |
813 | 541 if (_logDispatch) |
542 { | |
543 long d = request.getDispatchTime(); | |
544 buf.append(' '); | |
545 buf.append(now - (d==0 ? request.getTimeStamp():d)); | |
546 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
547 |
813 | 548 if (_logLatency) |
549 { | |
550 buf.append(' '); | |
551 buf.append(now - request.getTimeStamp()); | |
552 } | |
553 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
554 |
813 | 555 buf.append(StringUtil.__LINE_SEPARATOR); |
556 | |
557 String log = buf.toString(); | |
558 write(log); | |
559 } | |
560 catch (IOException e) | |
561 { | |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
815
diff
changeset
|
562 LOG.warn("",e); |
813 | 563 } |
564 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
565 |
813 | 566 /* ------------------------------------------------------------ */ |
567 protected void write(String log) throws IOException | |
568 { | |
569 synchronized(this) | |
570 { | |
571 if (_writer==null) | |
572 return; | |
573 _writer.write(log); | |
574 _writer.flush(); | |
575 } | |
576 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
577 |
813 | 578 |
579 /* ------------------------------------------------------------ */ | |
580 /** | |
581 * Writes extended request and response information to the output stream. | |
582 * | |
583 * @param request request object | |
584 * @param response response object | |
585 * @param b StringBuilder to write to | |
586 * @throws IOException | |
587 */ | |
588 protected void logExtended(Request request, | |
589 Response response, | |
590 StringBuilder b) throws IOException | |
591 { | |
592 String referer = request.getHeader(HttpHeaders.REFERER); | |
593 if (referer == null) | |
594 b.append("\"-\" "); | |
595 else | |
596 { | |
597 b.append('"'); | |
598 b.append(referer); | |
599 b.append("\" "); | |
600 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
601 |
813 | 602 String agent = request.getHeader(HttpHeaders.USER_AGENT); |
603 if (agent == null) | |
604 b.append("\"-\" "); | |
605 else | |
606 { | |
607 b.append('"'); | |
608 b.append(agent); | |
609 b.append('"'); | |
610 } | |
611 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
612 |
813 | 613 /* ------------------------------------------------------------ */ |
614 /** | |
615 * Set up request logging and open log file. | |
616 * | |
617 * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart() | |
618 */ | |
619 @Override | |
620 protected synchronized void doStart() throws Exception | |
621 { | |
622 if (_logDateFormat != null) | |
623 { | |
624 _logDateCache = new DateCache(_logDateFormat,_logLocale); | |
625 _logDateCache.setTimeZoneID(_logTimeZone); | |
626 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
627 |
813 | 628 if (_filename != null) |
629 { | |
630 _fileOut = new RolloverFileOutputStream(_filename,_append,_retainDays,TimeZone.getTimeZone(_logTimeZone),_filenameDateFormat,null); | |
631 _closeOut = true; | |
632 LOG.info("Opened " + getDatedFilename()); | |
633 } | |
634 else | |
635 _fileOut = System.err; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
636 |
813 | 637 _out = _fileOut; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
638 |
813 | 639 synchronized(this) |
640 { | |
641 _writer = new OutputStreamWriter(_out); | |
642 } | |
643 super.doStart(); | |
644 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
645 |
813 | 646 /* ------------------------------------------------------------ */ |
647 /** | |
648 * Close the log file and perform cleanup. | |
649 * | |
650 * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStop() | |
651 */ | |
652 @Override | |
653 protected void doStop() throws Exception | |
654 { | |
655 synchronized (this) | |
656 { | |
657 super.doStop(); | |
658 try | |
659 { | |
660 if (_writer != null) | |
661 _writer.flush(); | |
662 } | |
663 catch (IOException e) | |
664 { | |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
815
diff
changeset
|
665 LOG.trace("",e); |
813 | 666 } |
667 if (_out != null && _closeOut) | |
668 try | |
669 { | |
670 _out.close(); | |
671 } | |
672 catch (IOException e) | |
673 { | |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
815
diff
changeset
|
674 LOG.trace("",e); |
813 | 675 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
676 |
813 | 677 _out = null; |
678 _fileOut = null; | |
679 _closeOut = false; | |
680 _logDateCache = null; | |
681 _writer = null; | |
682 } | |
683 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
684 } |