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