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