comparison src/org/eclipse/jetty/server/NCSARequestLog.java @ 853:3242aff51053

remove RolloverFileOutputStream and cleanup NCSARequestLog
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 20 Sep 2016 00:23:56 -0600
parents 175577dab6d8
children 359012f4e797
comparison
equal deleted inserted replaced
852:b462b4ff22d8 853:3242aff51053
20 20
21 import java.io.IOException; 21 import java.io.IOException;
22 import java.io.OutputStream; 22 import java.io.OutputStream;
23 import java.io.OutputStreamWriter; 23 import java.io.OutputStreamWriter;
24 import java.io.Writer; 24 import java.io.Writer;
25 import java.io.File;
26 import java.io.FileOutputStream;
25 import java.util.Locale; 27 import java.util.Locale;
26 import java.util.TimeZone; 28 import java.util.TimeZone;
27 29
28 import javax.servlet.http.Cookie; 30 import javax.servlet.http.Cookie;
29 31
30 import org.eclipse.jetty.http.HttpHeaders; 32 import org.eclipse.jetty.http.HttpHeaders;
31 import org.eclipse.jetty.util.DateCache; 33 import org.eclipse.jetty.util.DateCache;
32 import org.eclipse.jetty.util.RolloverFileOutputStream;
33 import org.eclipse.jetty.util.StringUtil;
34 import org.eclipse.jetty.util.component.AbstractLifeCycle; 34 import org.eclipse.jetty.util.component.AbstractLifeCycle;
35 import org.slf4j.Logger; 35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory; 36 import org.slf4j.LoggerFactory;
37 37
38 /** 38 /**
50 /** 50 /**
51 */ 51 */
52 public class NCSARequestLog extends AbstractLifeCycle implements RequestLog 52 public class NCSARequestLog extends AbstractLifeCycle implements RequestLog
53 { 53 {
54 private static final Logger LOG = LoggerFactory.getLogger(NCSARequestLog.class); 54 private static final Logger LOG = LoggerFactory.getLogger(NCSARequestLog.class);
55 private static ThreadLocal<StringBuilder> _buffers = new ThreadLocal<StringBuilder>() 55 private static final String __LINE_SEPARATOR=
56 { 56 System.getProperty("line.separator","\n");
57 @Override
58 protected StringBuilder initialValue()
59 {
60 return new StringBuilder(256);
61 }
62 };
63 57
64 private String _filename; 58 private String _filename;
65 private boolean _extended; 59 private boolean _extended;
66 private boolean _append; 60 public long sizeLimit = 1048576L;
67 private int _retainDays;
68 private boolean _closeOut; 61 private boolean _closeOut;
69 private boolean _preferProxiedForAddress; 62 private boolean _preferProxiedForAddress;
70 private String _logDateFormat = "dd/MMM/yyyy:HH:mm:ss Z"; 63 private String _logDateFormat = "dd/MMM/yyyy:HH:mm:ss Z";
71 private String _filenameDateFormat = null;
72 private Locale _logLocale = Locale.getDefault(); 64 private Locale _logLocale = Locale.getDefault();
73 private String _logTimeZone = "GMT"; 65 private String _logTimeZone = "GMT";
74 private boolean _logLatency = false; 66 private boolean _logLatency = false;
75 private boolean _logCookies = false; 67 private boolean _logCookies = false;
76 private boolean _logServer = false; 68 private boolean _logServer = false;
77 private boolean _logDispatch = false; 69 private boolean _logDispatch = false;
78 70
79 private transient OutputStream _out; 71 private transient OutputStream _out;
80 private transient OutputStream _fileOut;
81 private transient DateCache _logDateCache; 72 private transient DateCache _logDateCache;
82 private transient Writer _writer; 73 private transient Writer _writer;
83 74
84 /* ------------------------------------------------------------ */ 75 /* ------------------------------------------------------------ */
85 /** 76 /**
86 * Create request log object with default settings. 77 * Create request log object with default settings.
87 */ 78 */
88 public NCSARequestLog() 79 public NCSARequestLog()
89 { 80 {
90 _extended = true; 81 _extended = true;
91 _append = true;
92 _retainDays = 31;
93 } 82 }
94 83
95 /* ------------------------------------------------------------ */ 84 /* ------------------------------------------------------------ */
96 /** 85 /**
97 * Create request log object with specified output file name. 86 * Create request log object with specified output file name.
101 * by {@link RolloverFileOutputStream} 90 * by {@link RolloverFileOutputStream}
102 */ 91 */
103 public NCSARequestLog(String filename) 92 public NCSARequestLog(String filename)
104 { 93 {
105 _extended = true; 94 _extended = true;
106 _append = true;
107 _retainDays = 31;
108 setFilename(filename); 95 setFilename(filename);
109 } 96 }
110 97
111 /* ------------------------------------------------------------ */ 98 /* ------------------------------------------------------------ */
112 /** 99 /**
139 return _filename; 126 return _filename;
140 } 127 }
141 128
142 /* ------------------------------------------------------------ */ 129 /* ------------------------------------------------------------ */
143 /** 130 /**
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 }
156
157 /* ------------------------------------------------------------ */
158 /**
159 * Set the timestamp format for request log entries in the file. 131 * Set the timestamp format for request log entries in the file.
160 * If this is not set, the pre-formated request timestamp is used. 132 * If this is not set, the pre-formated request timestamp is used.
161 * 133 *
162 * @param format timestamp format string 134 * @param format timestamp format string
163 */ 135 */
221 return _logTimeZone; 193 return _logTimeZone;
222 } 194 }
223 195
224 /* ------------------------------------------------------------ */ 196 /* ------------------------------------------------------------ */
225 /** 197 /**
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 }
234
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 }
245
246 /* ------------------------------------------------------------ */
247 /**
248 * Set the extended request log format flag. 198 * Set the extended request log format flag.
249 * 199 *
250 * @param extended true - log the extended request information, 200 * @param extended true - log the extended request information,
251 * false - do not log the extended request information 201 * false - do not log the extended request information
252 */ 202 */
266 return _extended; 216 return _extended;
267 } 217 }
268 218
269 /* ------------------------------------------------------------ */ 219 /* ------------------------------------------------------------ */
270 /** 220 /**
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 }
280
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 }
291
292 /* ------------------------------------------------------------ */
293 /**
294 * Controls logging of the request cookies. 221 * Controls logging of the request cookies.
295 * 222 *
296 * @param logCookies true - values of request cookies will be logged, 223 * @param logCookies true - values of request cookies will be logged,
297 * false - values of request cookies will not be logged 224 * false - values of request cookies will not be logged
298 */ 225 */
381 { 308 {
382 return _preferProxiedForAddress; 309 return _preferProxiedForAddress;
383 } 310 }
384 311
385 /* ------------------------------------------------------------ */ 312 /* ------------------------------------------------------------ */
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 }
396
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 }
407
408 /* ------------------------------------------------------------ */
409 /** 313 /**
410 * Controls logging of the request dispatch time 314 * Controls logging of the request dispatch time
411 * 315 *
412 * @param value true - request dispatch time will be logged 316 * @param value true - request dispatch time will be logged
413 * false - request dispatch time will not be logged 317 * false - request dispatch time will not be logged
436 */ 340 */
437 public void log(Request request, Response response) 341 public void log(Request request, Response response)
438 { 342 {
439 try 343 try
440 { 344 {
441 if (_fileOut == null) 345 if (_out == null)
442 return; 346 return;
443 347
444 StringBuilder buf= _buffers.get(); 348 StringBuilder buf = new StringBuilder(256);
445 buf.setLength(0);
446 349
447 if (_logServer) 350 if (_logServer)
448 { 351 {
449 buf.append(request.getServerName()); 352 buf.append(request.getServerName());
450 buf.append(' '); 353 buf.append(' ');
550 buf.append(' '); 453 buf.append(' ');
551 buf.append(now - request.getTimeStamp()); 454 buf.append(now - request.getTimeStamp());
552 } 455 }
553 } 456 }
554 457
555 buf.append(StringUtil.__LINE_SEPARATOR); 458 buf.append(__LINE_SEPARATOR);
556 459
557 String log = buf.toString(); 460 String log = buf.toString();
558 write(log); 461 write(log);
559 } 462 }
560 catch (IOException e) 463 catch (IOException e)
623 { 526 {
624 _logDateCache = new DateCache(_logDateFormat,_logLocale); 527 _logDateCache = new DateCache(_logDateFormat,_logLocale);
625 _logDateCache.setTimeZoneID(_logTimeZone); 528 _logDateCache.setTimeZoneID(_logTimeZone);
626 } 529 }
627 530
628 if (_filename != null) 531 if (_filename != null) {
629 { 532 File file = new File(_filename);
630 _fileOut = new RolloverFileOutputStream(_filename,_append,_retainDays,TimeZone.getTimeZone(_logTimeZone),_filenameDateFormat,null); 533 if( file.exists() && file.length() > sizeLimit ) {
534 File old = new File(_filename+".old");
535 old.delete();
536 file.renameTo(old);
537 }
538 _out = new FileOutputStream(file,true);
631 _closeOut = true; 539 _closeOut = true;
632 LOG.info("Opened " + getDatedFilename()); 540 LOG.info("Opened " + _filename);
633 } 541 }
634 else 542 else
635 _fileOut = System.err; 543 _out = System.err;
636
637 _out = _fileOut;
638 544
639 synchronized(this) 545 synchronized(this)
640 { 546 {
641 _writer = new OutputStreamWriter(_out); 547 _writer = new OutputStreamWriter(_out);
642 } 548 }
662 } 568 }
663 catch (IOException e) 569 catch (IOException e)
664 { 570 {
665 LOG.trace("",e); 571 LOG.trace("",e);
666 } 572 }
667 if (_out != null && _closeOut) 573 if (_closeOut)
668 try 574 try
669 { 575 {
670 _out.close(); 576 _out.close();
671 } 577 }
672 catch (IOException e) 578 catch (IOException e)
673 { 579 {
674 LOG.trace("",e); 580 LOG.trace("",e);
675 } 581 }
676 582
677 _out = null; 583 _out = null;
678 _fileOut = null;
679 _closeOut = false; 584 _closeOut = false;
680 _logDateCache = null; 585 _logDateCache = null;
681 _writer = null; 586 _writer = null;
682 } 587 }
683 } 588 }