2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server;
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.io.OutputStreamWriter;
24 import java.io.Writer;
25 import java.util.TimeZone;
27 import org.eclipse.jetty.util.RolloverFileOutputStream;
28 import org.eclipse.jetty.util.StringUtil;
29 import org.eclipse.jetty.util.annotation.ManagedAttribute;
30 import org.eclipse.jetty.util.annotation.ManagedObject;
33 * This {@link RequestLog} implementation outputs logs in the pseudo-standard
34 * NCSA common log format. Configuration options allow a choice between the
35 * standard Common Log Format (as used in the 3 log format) and the Combined Log
36 * Format (single log format). This log format can be output by most web
37 * servers, and almost all web log analysis software can understand these
40 @ManagedObject("NCSA standard format request log")
41 public class NCSARequestLog extends AbstractNCSARequestLog implements RequestLog
43 private String _filename;
44 private boolean _append;
45 private int _retainDays;
46 private boolean _closeOut;
47 private String _filenameDateFormat = null;
48 private transient OutputStream _out;
49 private transient OutputStream _fileOut;
50 private transient Writer _writer;
52 /* ------------------------------------------------------------ */
54 * Create request log object with default settings.
56 public NCSARequestLog()
63 /* ------------------------------------------------------------ */
65 * Create request log object with specified output file name.
67 * @param filename the file name for the request log.
68 * This may be in the format expected
69 * by {@link RolloverFileOutputStream}
71 public NCSARequestLog(String filename)
76 setFilename(filename);
79 /* ------------------------------------------------------------ */
81 * Set the output file name of the request log.
82 * The file name may be in the format expected by
83 * {@link RolloverFileOutputStream}.
85 * @param filename file name of the request log
88 public void setFilename(String filename)
92 filename = filename.trim();
93 if (filename.length() == 0)
99 /* ------------------------------------------------------------ */
101 * Retrieve the output file name of the request log.
103 * @return file name of the request log
105 @ManagedAttribute("file of log")
106 public String getFilename()
111 /* ------------------------------------------------------------ */
113 * Retrieve the file name of the request log with the expanded
114 * date wildcard if the output is written to the disk using
115 * {@link RolloverFileOutputStream}.
117 * @return file name of the request log, or null if not applicable
119 public String getDatedFilename()
121 if (_fileOut instanceof RolloverFileOutputStream)
122 return ((RolloverFileOutputStream)_fileOut).getDatedFilename();
126 /* ------------------------------------------------------------ */
128 protected boolean isEnabled()
130 return (_fileOut != null);
133 /* ------------------------------------------------------------ */
135 * Set the number of days before rotated log files are deleted.
137 * @param retainDays number of days to keep a log file
139 public void setRetainDays(int retainDays)
141 _retainDays = retainDays;
144 /* ------------------------------------------------------------ */
146 * Retrieve the number of days before rotated log files are deleted.
148 * @return number of days to keep a log file
150 @ManagedAttribute("number of days that log files are kept")
151 public int getRetainDays()
156 /* ------------------------------------------------------------ */
158 * Set append to log flag.
160 * @param append true - request log file will be appended after restart,
161 * false - request log file will be overwritten after restart
163 public void setAppend(boolean append)
168 /* ------------------------------------------------------------ */
170 * Retrieve append to log flag.
172 * @return value of the flag
174 @ManagedAttribute("existing log files are appends to the new one")
175 public boolean isAppend()
180 /* ------------------------------------------------------------ */
182 * Set the log file name date format.
183 * @see RolloverFileOutputStream#RolloverFileOutputStream(String, boolean, int, TimeZone, String, String)
185 * @param logFileDateFormat format string that is passed to {@link RolloverFileOutputStream}
187 public void setFilenameDateFormat(String logFileDateFormat)
189 _filenameDateFormat = logFileDateFormat;
192 /* ------------------------------------------------------------ */
194 * Retrieve the file name date format string.
196 * @return the log File Date Format
198 public String getFilenameDateFormat()
200 return _filenameDateFormat;
203 /* ------------------------------------------------------------ */
205 public void write(String requestEntry) throws IOException
211 _writer.write(requestEntry);
212 _writer.write(StringUtil.__LINE_SEPARATOR);
217 /* ------------------------------------------------------------ */
219 * Set up request logging and open log file.
221 * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart()
224 protected synchronized void doStart() throws Exception
226 if (_filename != null)
228 _fileOut = new RolloverFileOutputStream(_filename,_append,_retainDays,TimeZone.getTimeZone(getLogTimeZone()),_filenameDateFormat,null);
230 LOG.info("Opened " + getDatedFilename());
233 _fileOut = System.err;
239 _writer = new OutputStreamWriter(_out);
244 /* ------------------------------------------------------------ */
246 * Close the log file and perform cleanup.
248 * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStop()
251 protected void doStop() throws Exception
261 catch (IOException e)
265 if (_out != null && _closeOut)
270 catch (IOException e)