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