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