Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/NCSARequestLog.java @ 855:fa6158f29c45
remove BufferDateCache and DateCache
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 20 Sep 2016 21:38:44 -0600 |
parents | 359012f4e797 |
children |
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; |
853
3242aff51053
remove RolloverFileOutputStream and cleanup NCSARequestLog
Franklin Schmidt <fschmidt@gmail.com>
parents:
834
diff
changeset
|
25 import java.io.File; |
3242aff51053
remove RolloverFileOutputStream and cleanup NCSARequestLog
Franklin Schmidt <fschmidt@gmail.com>
parents:
834
diff
changeset
|
26 import java.io.FileOutputStream; |
855
fa6158f29c45
remove BufferDateCache and DateCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
854
diff
changeset
|
27 import java.text.DateFormat; |
fa6158f29c45
remove BufferDateCache and DateCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
854
diff
changeset
|
28 import java.text.SimpleDateFormat; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 import java.util.Locale; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 import java.util.TimeZone; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 import javax.servlet.http.Cookie; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 import org.eclipse.jetty.http.HttpHeaders; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 import org.eclipse.jetty.util.component.AbstractLifeCycle; |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
815
diff
changeset
|
36 import org.slf4j.Logger; |
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
815
diff
changeset
|
37 import org.slf4j.LoggerFactory; |
802
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 { |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
815
diff
changeset
|
55 private static final Logger LOG = LoggerFactory.getLogger(NCSARequestLog.class); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
56 |
813 | 57 private String _filename; |
58 private boolean _extended; | |
854 | 59 public long retainTime = 1000L*60*60*24*31; // 31 days |
813 | 60 private boolean _closeOut; |
61 private boolean _preferProxiedForAddress; | |
62 private String _logDateFormat = "dd/MMM/yyyy:HH:mm:ss Z"; | |
63 private Locale _logLocale = Locale.getDefault(); | |
855
fa6158f29c45
remove BufferDateCache and DateCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
854
diff
changeset
|
64 public TimeZone timeZone = TimeZone.getTimeZone("GMT"); |
813 | 65 private boolean _logLatency = false; |
66 private boolean _logCookies = false; | |
67 private boolean _logServer = false; | |
68 private boolean _logDispatch = false; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
69 |
813 | 70 private transient OutputStream _out; |
855
fa6158f29c45
remove BufferDateCache and DateCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
854
diff
changeset
|
71 private transient DateFormat dateFormat; |
813 | 72 private transient Writer _writer; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
73 |
813 | 74 /* ------------------------------------------------------------ */ |
75 /** | |
76 * Create request log object with default settings. | |
77 */ | |
78 public NCSARequestLog() | |
79 { | |
80 _extended = true; | |
81 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
82 |
813 | 83 /* ------------------------------------------------------------ */ |
84 /** | |
85 * Create request log object with specified output file name. | |
86 * | |
87 * @param filename the file name for the request log. | |
88 * This may be in the format expected | |
89 * by {@link RolloverFileOutputStream} | |
90 */ | |
91 public NCSARequestLog(String filename) | |
92 { | |
93 _extended = true; | |
94 setFilename(filename); | |
95 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
96 |
813 | 97 /* ------------------------------------------------------------ */ |
98 /** | |
99 * Set the output file name of the request log. | |
100 * The file name may be in the format expected by | |
101 * {@link RolloverFileOutputStream}. | |
102 * | |
103 * @param filename file name of the request log | |
104 * | |
105 */ | |
106 public void setFilename(String filename) | |
107 { | |
108 if (filename != null) | |
109 { | |
110 filename = filename.trim(); | |
111 if (filename.length() == 0) | |
112 filename = null; | |
113 } | |
114 _filename = filename; | |
115 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
116 |
813 | 117 /* ------------------------------------------------------------ */ |
118 /** | |
119 * Retrieve the output file name of the request log. | |
120 * | |
121 * @return file name of the request log | |
122 */ | |
123 public String getFilename() | |
124 { | |
125 return _filename; | |
126 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
127 |
813 | 128 /* ------------------------------------------------------------ */ |
129 /** | |
130 * Set the timestamp format for request log entries in the file. | |
131 * If this is not set, the pre-formated request timestamp is used. | |
132 * | |
133 * @param format timestamp format string | |
134 */ | |
135 public void setLogDateFormat(String format) | |
136 { | |
137 _logDateFormat = format; | |
138 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
139 |
813 | 140 /* ------------------------------------------------------------ */ |
141 /** | |
142 * Retrieve the timestamp format string for request log entries. | |
143 * | |
144 * @return timestamp format string. | |
145 */ | |
146 public String getLogDateFormat() | |
147 { | |
148 return _logDateFormat; | |
149 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
150 |
813 | 151 /* ------------------------------------------------------------ */ |
152 /** | |
153 * Set the locale of the request log. | |
154 * | |
155 * @param logLocale locale object | |
156 */ | |
157 public void setLogLocale(Locale logLocale) | |
158 { | |
159 _logLocale = logLocale; | |
160 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
161 |
813 | 162 /* ------------------------------------------------------------ */ |
163 /** | |
164 * Retrieve the locale of the request log. | |
165 * | |
166 * @return locale object | |
167 */ | |
168 public Locale getLogLocale() | |
169 { | |
170 return _logLocale; | |
171 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
172 |
813 | 173 /* ------------------------------------------------------------ */ |
174 /** | |
175 * Set the extended request log format flag. | |
176 * | |
177 * @param extended true - log the extended request information, | |
178 * false - do not log the extended request information | |
179 */ | |
180 public void setExtended(boolean extended) | |
181 { | |
182 _extended = extended; | |
183 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
184 |
813 | 185 /* ------------------------------------------------------------ */ |
186 /** | |
187 * Retrieve the extended request log format flag. | |
188 * | |
189 * @return value of the flag | |
190 */ | |
191 public boolean isExtended() | |
192 { | |
193 return _extended; | |
194 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
195 |
813 | 196 /* ------------------------------------------------------------ */ |
197 /** | |
198 * Controls logging of the request cookies. | |
199 * | |
200 * @param logCookies true - values of request cookies will be logged, | |
201 * false - values of request cookies will not be logged | |
202 */ | |
203 public void setLogCookies(boolean logCookies) | |
204 { | |
205 _logCookies = logCookies; | |
206 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
207 |
813 | 208 /* ------------------------------------------------------------ */ |
209 /** | |
210 * Retrieve log cookies flag | |
211 * | |
212 * @return value of the flag | |
213 */ | |
214 public boolean getLogCookies() | |
215 { | |
216 return _logCookies; | |
217 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
218 |
813 | 219 /* ------------------------------------------------------------ */ |
220 /** | |
221 * Controls logging of the request hostname. | |
222 * | |
223 * @param logServer true - request hostname will be logged, | |
224 * false - request hostname will not be logged | |
225 */ | |
226 public void setLogServer(boolean logServer) | |
227 { | |
228 _logServer = logServer; | |
229 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
230 |
813 | 231 /* ------------------------------------------------------------ */ |
232 /** | |
233 * Retrieve log hostname flag. | |
234 * | |
235 * @return value of the flag | |
236 */ | |
237 public boolean getLogServer() | |
238 { | |
239 return _logServer; | |
240 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
241 |
813 | 242 /* ------------------------------------------------------------ */ |
243 /** | |
244 * Controls logging of request processing time. | |
245 * | |
246 * @param logLatency true - request processing time will be logged | |
247 * false - request processing time will not be logged | |
248 */ | |
249 public void setLogLatency(boolean logLatency) | |
250 { | |
251 _logLatency = logLatency; | |
252 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
253 |
813 | 254 /* ------------------------------------------------------------ */ |
255 /** | |
256 * Retrieve log request processing time flag. | |
257 * | |
258 * @return value of the flag | |
259 */ | |
260 public boolean getLogLatency() | |
261 { | |
262 return _logLatency; | |
263 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
264 |
813 | 265 /* ------------------------------------------------------------ */ |
266 /** | |
267 * Controls whether the actual IP address of the connection or | |
268 * the IP address from the X-Forwarded-For header will be logged. | |
269 * | |
270 * @param preferProxiedForAddress true - IP address from header will be logged, | |
271 * false - IP address from the connection will be logged | |
272 */ | |
273 public void setPreferProxiedForAddress(boolean preferProxiedForAddress) | |
274 { | |
275 _preferProxiedForAddress = preferProxiedForAddress; | |
276 } | |
277 | |
278 /* ------------------------------------------------------------ */ | |
279 /** | |
280 * Retrieved log X-Forwarded-For IP address flag. | |
281 * | |
282 * @return value of the flag | |
283 */ | |
284 public boolean getPreferProxiedForAddress() | |
285 { | |
286 return _preferProxiedForAddress; | |
287 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
288 |
813 | 289 /* ------------------------------------------------------------ */ |
290 /** | |
291 * Controls logging of the request dispatch time | |
292 * | |
293 * @param value true - request dispatch time will be logged | |
294 * false - request dispatch time will not be logged | |
295 */ | |
296 public void setLogDispatch(boolean value) | |
297 { | |
298 _logDispatch = value; | |
299 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
300 |
813 | 301 /* ------------------------------------------------------------ */ |
302 /** | |
303 * Retrieve request dispatch time logging flag | |
304 * | |
305 * @return value of the flag | |
306 */ | |
307 public boolean isLogDispatch() | |
308 { | |
309 return _logDispatch; | |
310 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
311 |
813 | 312 /* ------------------------------------------------------------ */ |
313 /** | |
314 * Writes the request and response information to the output stream. | |
315 * | |
316 * @see org.eclipse.jetty.server.RequestLog#log(org.eclipse.jetty.server.Request, org.eclipse.jetty.server.Response) | |
317 */ | |
318 public void log(Request request, Response response) | |
319 { | |
320 try | |
321 { | |
853
3242aff51053
remove RolloverFileOutputStream and cleanup NCSARequestLog
Franklin Schmidt <fschmidt@gmail.com>
parents:
834
diff
changeset
|
322 if (_out == null) |
813 | 323 return; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
324 |
853
3242aff51053
remove RolloverFileOutputStream and cleanup NCSARequestLog
Franklin Schmidt <fschmidt@gmail.com>
parents:
834
diff
changeset
|
325 StringBuilder buf = new StringBuilder(256); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
326 |
813 | 327 if (_logServer) |
328 { | |
329 buf.append(request.getServerName()); | |
330 buf.append(' '); | |
331 } | |
332 | |
333 String addr = null; | |
334 if (_preferProxiedForAddress) | |
335 { | |
336 addr = request.getHeader(HttpHeaders.X_FORWARDED_FOR); | |
337 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
338 |
813 | 339 if (addr == null) |
340 addr = request.getRemoteAddr(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
341 |
813 | 342 buf.append(addr); |
855
fa6158f29c45
remove BufferDateCache and DateCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
854
diff
changeset
|
343 buf.append(" - ["); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
344 |
855
fa6158f29c45
remove BufferDateCache and DateCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
854
diff
changeset
|
345 synchronized(dateFormat) { |
fa6158f29c45
remove BufferDateCache and DateCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
854
diff
changeset
|
346 buf.append(dateFormat.format(request.getTimeStamp())); |
fa6158f29c45
remove BufferDateCache and DateCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
854
diff
changeset
|
347 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
348 |
813 | 349 buf.append("] \""); |
350 buf.append(request.getMethod()); | |
351 buf.append(' '); | |
352 buf.append(request.getUri().toString()); | |
353 buf.append(' '); | |
354 buf.append(request.getProtocol()); | |
355 buf.append("\" "); | |
356 if (request.getAsyncContinuation().isInitial()) | |
357 { | |
358 int status = response.getStatus(); | |
359 if (status <= 0) | |
360 status = 404; | |
361 buf.append((char)('0' + ((status / 100) % 10))); | |
362 buf.append((char)('0' + ((status / 10) % 10))); | |
363 buf.append((char)('0' + (status % 10))); | |
364 } | |
365 else | |
366 buf.append("Async"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
367 |
813 | 368 long responseLength = response.getContentCount(); |
369 if (responseLength >= 0) | |
370 { | |
371 buf.append(' '); | |
372 if (responseLength > 99999) | |
373 buf.append(responseLength); | |
374 else | |
375 { | |
376 if (responseLength > 9999) | |
377 buf.append((char)('0' + ((responseLength / 10000) % 10))); | |
378 if (responseLength > 999) | |
379 buf.append((char)('0' + ((responseLength / 1000) % 10))); | |
380 if (responseLength > 99) | |
381 buf.append((char)('0' + ((responseLength / 100) % 10))); | |
382 if (responseLength > 9) | |
383 buf.append((char)('0' + ((responseLength / 10) % 10))); | |
384 buf.append((char)('0' + (responseLength) % 10)); | |
385 } | |
386 buf.append(' '); | |
387 } | |
388 else | |
389 buf.append(" - "); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
390 |
813 | 391 |
392 if (_extended) | |
393 logExtended(request, response, buf); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
394 |
813 | 395 if (_logCookies) |
396 { | |
397 Cookie[] cookies = request.getCookies(); | |
398 if (cookies == null || cookies.length == 0) | |
399 buf.append(" -"); | |
400 else | |
401 { | |
402 buf.append(" \""); | |
403 for (int i = 0; i < cookies.length; i++) | |
404 { | |
405 if (i != 0) | |
406 buf.append(';'); | |
407 buf.append(cookies[i].getName()); | |
408 buf.append('='); | |
409 buf.append(cookies[i].getValue()); | |
410 } | |
411 buf.append('\"'); | |
412 } | |
413 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
414 |
813 | 415 if (_logDispatch || _logLatency) |
416 { | |
417 long now = System.currentTimeMillis(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
418 |
813 | 419 if (_logDispatch) |
420 { | |
421 long d = request.getDispatchTime(); | |
422 buf.append(' '); | |
423 buf.append(now - (d==0 ? request.getTimeStamp():d)); | |
424 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
425 |
813 | 426 if (_logLatency) |
427 { | |
428 buf.append(' '); | |
429 buf.append(now - request.getTimeStamp()); | |
430 } | |
431 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
432 |
854 | 433 buf.append('\n'); |
813 | 434 |
435 String log = buf.toString(); | |
436 write(log); | |
437 } | |
438 catch (IOException e) | |
439 { | |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
815
diff
changeset
|
440 LOG.warn("",e); |
813 | 441 } |
442 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
443 |
813 | 444 /* ------------------------------------------------------------ */ |
445 protected void write(String log) throws IOException | |
446 { | |
447 synchronized(this) | |
448 { | |
449 if (_writer==null) | |
450 return; | |
451 _writer.write(log); | |
452 _writer.flush(); | |
453 } | |
454 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
455 |
813 | 456 |
457 /* ------------------------------------------------------------ */ | |
458 /** | |
459 * Writes extended request and response information to the output stream. | |
460 * | |
461 * @param request request object | |
462 * @param response response object | |
463 * @param b StringBuilder to write to | |
464 * @throws IOException | |
465 */ | |
466 protected void logExtended(Request request, | |
467 Response response, | |
468 StringBuilder b) throws IOException | |
469 { | |
470 String referer = request.getHeader(HttpHeaders.REFERER); | |
471 if (referer == null) | |
472 b.append("\"-\" "); | |
473 else | |
474 { | |
475 b.append('"'); | |
476 b.append(referer); | |
477 b.append("\" "); | |
478 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
479 |
813 | 480 String agent = request.getHeader(HttpHeaders.USER_AGENT); |
481 if (agent == null) | |
482 b.append("\"-\" "); | |
483 else | |
484 { | |
485 b.append('"'); | |
486 b.append(agent); | |
487 b.append('"'); | |
488 } | |
489 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
490 |
813 | 491 /* ------------------------------------------------------------ */ |
492 /** | |
493 * Set up request logging and open log file. | |
494 * | |
495 * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart() | |
496 */ | |
497 @Override | |
498 protected synchronized void doStart() throws Exception | |
499 { | |
855
fa6158f29c45
remove BufferDateCache and DateCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
854
diff
changeset
|
500 dateFormat = new SimpleDateFormat(_logDateFormat,_logLocale); |
fa6158f29c45
remove BufferDateCache and DateCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
854
diff
changeset
|
501 dateFormat.setTimeZone(timeZone); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
502 |
853
3242aff51053
remove RolloverFileOutputStream and cleanup NCSARequestLog
Franklin Schmidt <fschmidt@gmail.com>
parents:
834
diff
changeset
|
503 if (_filename != null) { |
3242aff51053
remove RolloverFileOutputStream and cleanup NCSARequestLog
Franklin Schmidt <fschmidt@gmail.com>
parents:
834
diff
changeset
|
504 File file = new File(_filename); |
854 | 505 if( file.exists() ) { |
853
3242aff51053
remove RolloverFileOutputStream and cleanup NCSARequestLog
Franklin Schmidt <fschmidt@gmail.com>
parents:
834
diff
changeset
|
506 File old = new File(_filename+".old"); |
854 | 507 if( old.exists() && file.lastModified() - old.lastModified() > retainTime ) |
508 old.delete(); | |
509 if( !old.exists() ) | |
510 file.renameTo(old); | |
853
3242aff51053
remove RolloverFileOutputStream and cleanup NCSARequestLog
Franklin Schmidt <fschmidt@gmail.com>
parents:
834
diff
changeset
|
511 } |
3242aff51053
remove RolloverFileOutputStream and cleanup NCSARequestLog
Franklin Schmidt <fschmidt@gmail.com>
parents:
834
diff
changeset
|
512 _out = new FileOutputStream(file,true); |
813 | 513 _closeOut = true; |
853
3242aff51053
remove RolloverFileOutputStream and cleanup NCSARequestLog
Franklin Schmidt <fschmidt@gmail.com>
parents:
834
diff
changeset
|
514 LOG.info("Opened " + _filename); |
813 | 515 } |
516 else | |
853
3242aff51053
remove RolloverFileOutputStream and cleanup NCSARequestLog
Franklin Schmidt <fschmidt@gmail.com>
parents:
834
diff
changeset
|
517 _out = System.err; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
518 |
813 | 519 synchronized(this) |
520 { | |
521 _writer = new OutputStreamWriter(_out); | |
522 } | |
523 super.doStart(); | |
524 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
525 |
813 | 526 /* ------------------------------------------------------------ */ |
527 /** | |
528 * Close the log file and perform cleanup. | |
529 * | |
530 * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStop() | |
531 */ | |
532 @Override | |
533 protected void doStop() throws Exception | |
534 { | |
535 synchronized (this) | |
536 { | |
537 super.doStop(); | |
538 try | |
539 { | |
540 if (_writer != null) | |
541 _writer.flush(); | |
542 } | |
543 catch (IOException e) | |
544 { | |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
815
diff
changeset
|
545 LOG.trace("",e); |
813 | 546 } |
853
3242aff51053
remove RolloverFileOutputStream and cleanup NCSARequestLog
Franklin Schmidt <fschmidt@gmail.com>
parents:
834
diff
changeset
|
547 if (_closeOut) |
813 | 548 try |
549 { | |
550 _out.close(); | |
551 } | |
552 catch (IOException e) | |
553 { | |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
815
diff
changeset
|
554 LOG.trace("",e); |
813 | 555 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
556 |
813 | 557 _out = null; |
558 _closeOut = false; | |
855
fa6158f29c45
remove BufferDateCache and DateCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
854
diff
changeset
|
559 dateFormat = null; |
813 | 560 _writer = null; |
561 } | |
562 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
563 } |