2 // ========================================================================
3 // Copyright (c) 1995-2016 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.util.Locale;
24 import javax.servlet.http.Cookie;
26 import org.eclipse.jetty.http.HttpHeader;
27 import org.eclipse.jetty.http.PathMap;
28 import org.eclipse.jetty.util.DateCache;
29 import org.eclipse.jetty.util.annotation.ManagedAttribute;
30 import org.eclipse.jetty.util.component.AbstractLifeCycle;
31 import org.eclipse.jetty.util.log.Log;
32 import org.eclipse.jetty.util.log.Logger;
35 * Base implementation of the {@link RequestLog} outputs logs in the pseudo-standard NCSA common log format.
36 * Configuration options allow a choice between the standard Common Log Format (as used in the 3 log format) and the
37 * Combined Log Format (single log format). This log format can be output by most web servers, and almost all web log
38 * analysis software can understand these formats.
40 public abstract class AbstractNCSARequestLog extends AbstractLifeCycle implements RequestLog
42 protected static final Logger LOG = Log.getLogger(AbstractNCSARequestLog.class);
44 private static ThreadLocal<StringBuilder> _buffers = new ThreadLocal<StringBuilder>()
47 protected StringBuilder initialValue()
49 return new StringBuilder(256);
54 private String[] _ignorePaths;
55 private boolean _extended;
56 private transient PathMap<String> _ignorePathMap;
57 private boolean _logLatency = false;
58 private boolean _logCookies = false;
59 private boolean _logServer = false;
60 private boolean _preferProxiedForAddress;
61 private transient DateCache _logDateCache;
62 private String _logDateFormat = "dd/MMM/yyyy:HH:mm:ss Z";
63 private Locale _logLocale = Locale.getDefault();
64 private String _logTimeZone = "GMT";
66 /* ------------------------------------------------------------ */
71 protected abstract boolean isEnabled();
73 /* ------------------------------------------------------------ */
76 * Write requestEntry out. (to disk or slf4j log)
78 public abstract void write(String requestEntry) throws IOException;
80 /* ------------------------------------------------------------ */
83 * Writes the request and response information to the output stream.
85 * @see org.eclipse.jetty.server.RequestLog#log(org.eclipse.jetty.server.Request,
86 * org.eclipse.jetty.server.Response)
89 public void log(Request request, Response response)
93 if (_ignorePathMap != null && _ignorePathMap.getMatch(request.getRequestURI()) != null)
99 StringBuilder buf = _buffers.get();
104 buf.append(request.getServerName());
109 if (_preferProxiedForAddress)
111 addr = request.getHeader(HttpHeader.X_FORWARDED_FOR.toString());
115 addr = request.getRemoteAddr();
119 Authentication authentication = request.getAuthentication();
120 if (authentication instanceof Authentication.User)
121 buf.append(((Authentication.User)authentication).getUserIdentity().getUserPrincipal().getName());
126 if (_logDateCache != null)
127 buf.append(_logDateCache.format(request.getTimeStamp()));
129 buf.append(request.getTimeStamp());
132 buf.append(request.getMethod());
134 buf.append(request.getUri().toString());
136 buf.append(request.getProtocol());
139 int status = response.getStatus();
142 buf.append((char)('0' + ((status / 100) % 10)));
143 buf.append((char)('0' + ((status / 10) % 10)));
144 buf.append((char)('0' + (status % 10)));
146 long responseLength = response.getLongContentLength();
147 if (responseLength >= 0)
150 if (responseLength > 99999)
151 buf.append(responseLength);
154 if (responseLength > 9999)
155 buf.append((char)('0' + ((responseLength / 10000) % 10)));
156 if (responseLength > 999)
157 buf.append((char)('0' + ((responseLength / 1000) % 10)));
158 if (responseLength > 99)
159 buf.append((char)('0' + ((responseLength / 100) % 10)));
160 if (responseLength > 9)
161 buf.append((char)('0' + ((responseLength / 10) % 10)));
162 buf.append((char)('0' + (responseLength) % 10));
171 logExtended(request, response, buf);
175 Cookie[] cookies = request.getCookies();
176 if (cookies == null || cookies.length == 0)
181 for (int i = 0; i < cookies.length; i++)
185 buf.append(cookies[i].getName());
187 buf.append(cookies[i].getValue());
195 long now = System.currentTimeMillis();
200 buf.append(now - request.getTimeStamp());
204 String log = buf.toString();
207 catch (IOException e)
213 /* ------------------------------------------------------------ */
216 * Writes extended request and response information to the output stream.
218 * @param request request object
219 * @param response response object
220 * @param b StringBuilder to write to
221 * @throws IOException
223 protected void logExtended(Request request,
225 StringBuilder b) throws IOException
227 String referer = request.getHeader(HttpHeader.REFERER.toString());
237 String agent = request.getHeader(HttpHeader.USER_AGENT.toString());
250 * Set request paths that will not be logged.
252 * @param ignorePaths array of request paths
254 public void setIgnorePaths(String[] ignorePaths)
256 _ignorePaths = ignorePaths;
260 * Retrieve the request paths that will not be logged.
262 * @return array of request paths
264 public String[] getIgnorePaths()
270 * Controls logging of the request cookies.
272 * @param logCookies true - values of request cookies will be logged, false - values of request cookies will not be
275 public void setLogCookies(boolean logCookies)
277 _logCookies = logCookies;
281 * Retrieve log cookies flag
283 * @return value of the flag
285 public boolean getLogCookies()
291 * Controls logging of the request hostname.
293 * @param logServer true - request hostname will be logged, false - request hostname will not be logged
295 public void setLogServer(boolean logServer)
297 _logServer = logServer;
301 * Retrieve log hostname flag.
303 * @return value of the flag
305 public boolean getLogServer()
311 * Controls logging of request processing time.
313 * @param logLatency true - request processing time will be logged false - request processing time will not be
316 public void setLogLatency(boolean logLatency)
318 _logLatency = logLatency;
322 * Retrieve log request processing time flag.
324 * @return value of the flag
326 public boolean getLogLatency()
332 * @deprecated use {@link StatisticsHandler}
334 public void setLogDispatch(boolean value)
339 * @deprecated use {@link StatisticsHandler}
341 public boolean isLogDispatch()
347 * Controls whether the actual IP address of the connection or the IP address from the X-Forwarded-For header will
350 * @param preferProxiedForAddress true - IP address from header will be logged, false - IP address from the
351 * connection will be logged
353 public void setPreferProxiedForAddress(boolean preferProxiedForAddress)
355 _preferProxiedForAddress = preferProxiedForAddress;
359 * Retrieved log X-Forwarded-For IP address flag.
361 * @return value of the flag
363 public boolean getPreferProxiedForAddress()
365 return _preferProxiedForAddress;
369 * Set the extended request log format flag.
371 * @param extended true - log the extended request information, false - do not log the extended request information
373 public void setExtended(boolean extended)
375 _extended = extended;
379 * Retrieve the extended request log format flag.
381 * @return value of the flag
383 @ManagedAttribute("use extended NCSA format")
384 public boolean isExtended()
390 * Set up request logging and open log file.
392 * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart()
395 protected synchronized void doStart() throws Exception
397 if (_logDateFormat != null)
399 _logDateCache = new DateCache(_logDateFormat, _logLocale ,_logTimeZone);
402 if (_ignorePaths != null && _ignorePaths.length > 0)
404 _ignorePathMap = new PathMap<>();
405 for (int i = 0; i < _ignorePaths.length; i++)
406 _ignorePathMap.put(_ignorePaths[i], _ignorePaths[i]);
409 _ignorePathMap = null;
415 protected void doStop() throws Exception
417 _logDateCache = null;
422 * Set the timestamp format for request log entries in the file. If this is not set, the pre-formated request
425 * @param format timestamp format string
427 public void setLogDateFormat(String format)
429 _logDateFormat = format;
433 * Retrieve the timestamp format string for request log entries.
435 * @return timestamp format string.
437 public String getLogDateFormat()
439 return _logDateFormat;
443 * Set the locale of the request log.
445 * @param logLocale locale object
447 public void setLogLocale(Locale logLocale)
449 _logLocale = logLocale;
453 * Retrieve the locale of the request log.
455 * @return locale object
457 public Locale getLogLocale()
463 * Set the timezone of the request log.
465 * @param tz timezone string
467 public void setLogTimeZone(String tz)
473 * Retrieve the timezone of the request log.
475 * @return timezone string
477 @ManagedAttribute("the timezone")
478 public String getLogTimeZone()