r110186 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110185‎ | r110186 | r110187 >
Date:23:19, 27 January 2012
Author:oren
Status:deferred
Tags:
Comment:
improvements to logging
Modified paths:
  • /trunk/lucene-search-3/src/main/java/org/wikimedia/lsearch/frontend/HttpHandler.java (modified) (history)

Diff [purge]

Index: trunk/lucene-search-3/src/main/java/org/wikimedia/lsearch/frontend/HttpHandler.java
@@ -14,23 +14,31 @@
1515 import java.net.URI;
1616 import java.net.URISyntaxException;
1717 import java.util.HashMap;
 18+
 19+import org.apache.log4j.Level;
1820 import org.apache.log4j.Logger;
1921
2022 /**
2123 * Simple HTTP 1.1 handler, used for Index and Search daemons
2224 * for more info about the protocol see handle() method
2325 *
 26+ *
 27+ * NOTE: we are using old JDK 1.1 classes which don't have
 28+ * a very good unicode support, so extra care needs to be taken
 29+ * so everything is converted out right. The old classes are used
 30+ * because they enable better byte-oriented operations.
 31+ *
 32+ * $LastChangedDate:$
 33+ * $LastChangedRevision:$
 34+ * $LastChangedBy:$
 35+ *
2436 * @author Brion Vibber
2537 *
2638 */
2739 abstract public class HttpHandler extends Thread {
2840 protected static org.apache.log4j.Logger log = Logger.getLogger(HttpHandler.class);
2941
30 - /** NOTE: we are using old JDK 1.1 classes which don't have
31 - * a very good unicode support, so extra care needs to be taken
32 - * so everything is converted out right. The old classes are used
33 - * because they enable better byte-oriented operations.
34 - */
 42+
3543 /** Client input stream */
3644 DataInputStream istrm;
3745 /** Client output stream */
@@ -52,6 +60,10 @@
5361 protected String charset = "none";
5462 boolean headersSent;
5563
 64+ /**
 65+ * collection of the headers
 66+ */
 67+ @SuppressWarnings("rawtypes")
5668 protected HashMap<String, Comparable> headers;
5769
5870 protected static HttpMonitor monitor = null;
@@ -63,7 +75,9 @@
6476 istrm = new DataInputStream(new BufferedInputStream(s.getInputStream()));
6577 ostrm = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream(),"utf-8")));
6678 } catch (IOException e) {
67 - log.error("I/O in opening http socket.",e);
 79+ if (log.isEnabledFor(Level.ERROR)){
 80+ log.error("I/O in opening http socket.",e);
 81+ }
6882 }
6983 }
7084
@@ -91,7 +105,7 @@
92106 public boolean isKeepAlive(){
93107 if(version.equals("HTTP/1.0")){
94108 if(headers.get("Connection")!=null &&
95 - ((String)headers.get("Connection")).equalsIgnoreCase("Keep-Alive"))
 109+ ((String)headers.get("Connection")).equalsIgnoreCase("keep-alive"))
96110 return true;
97111 else
98112 return false;
@@ -112,12 +126,19 @@
113127 do{
114128 headersSent = false;
115129 handle();
116 - log.debug("request handled.");
 130+ if (log.isEnabledFor(Level.DEBUG)){
 131+ log.debug("request handled.");
 132+ }
 133+
117134 } while(isKeepAlive());
118 - log.debug("No keep-alive, closing connection ... ");
 135+ if (log.isEnabledFor(Level.DEBUG)){
 136+ log.debug("No keep-alive, closing connection ... ");
 137+ }
119138 } catch (Exception e) {
120139 e.printStackTrace();
121 - log.error(e.getMessage(),e);
 140+ if (log.isEnabledFor(Level.DEBUG)){
 141+ log.error(e.getMessage(),e);
 142+ }
122143 } finally {
123144 if (!headersSent) {
124145 sendError(500, "Internal server error", "An internal error occurred: no header sent.");
@@ -133,7 +154,7 @@
134155 /**
135156 * Simple HTTP protocol; Used for search (GET) and indexing (POST)
136157 * GET requests syntax:
137 - * URL path format: /operation/database/searchterm
 158+ * URL path format: /operation/database/search-term
138159 * The path should be URL-encoded UTF-8 (standard IRI).
139160 *
140161 * Additional parameters may be specified in a query string:
@@ -148,7 +169,9 @@
149170 * typically, this means: /updatePage?db=entest&title=Main%20Page
150171 * and the POST content is article text
151172 */
 173+ @SuppressWarnings("rawtypes")
152174 protected void handle() {
 175+
153176 headers = new HashMap<String, Comparable>();
154177
155178 // parse first line
@@ -176,9 +199,10 @@
177200 try {
178201 uri = new URI("http://localhost:8123" + rawUri);
179202 } catch (URISyntaxException e) {
180 - sendError(400, "Bad Request",
181 - "Couldn't make sense of the given URI.");
182 - log.warn("Bad URI in request: " + rawUri,e);
 203+ sendError(400, "Bad Request", "Couldn't make sense of the given URI.");
 204+ if (log.isEnabledFor(Level.WARN)) {
 205+ log.warn("Bad URI in request: " + rawUri,e);
 206+ }
183207 return;
184208 }
185209
@@ -203,7 +227,9 @@
204228
205229 protected void sendHeaders(int code, String message, int contentLen) {
206230 if (headersSent) {
207 - log.warn("Asked to send headers, but already sent! ("+code+" "+message+")");
 231+ if (log.isEnabledFor(Level.WARN)){
 232+ log.warn("Asked to send headers, but already sent! ("+code+" "+message+")");
 233+ }
208234 return;
209235 }
210236 sendOutputLine("HTTP/1.1 "+code+" "+message);
@@ -232,7 +258,9 @@
233259
234260 /** Send single line to client. The lines are buffered and sent out in chunks */
235261 protected void sendOutputLine(String sout) {
236 - log.debug(">>>"+sout);
 262+ if (log.isEnabledFor(Level.DEBUG)){
 263+ log.debug(">>>"+sout);
 264+ }
237265 // write to buffer instead directly to stream!
238266 char[] s = (sout+"\r\n").toCharArray();
239267 if(bufLength + s.length >= outputBuffer.length)
@@ -247,7 +275,9 @@
248276
249277 /** Sending raw data to client */
250278 protected void sendBytes(char[] data){
251 - log.debug(">>> Writing "+data.length+" bytes of data");
 279+ if (log.isEnabledFor(Level.DEBUG)){
 280+ log.debug(">>> Writing "+data.length+" bytes of data");
 281+ }
252282 flushOutput();
253283 ostrm.write(data);
254284 }
@@ -266,7 +296,9 @@
267297 //log.error("Internal error, read "+read+" bytes istead of "+contentLength+" from POST request");
268298 return data;
269299 } catch (IOException e) {
270 - log.warn("Could not send raw data in bytes to output stream.",e);
 300+ if (log.isEnabledFor(Level.WARN)){
 301+ log.warn("Could not send raw data in bytes to output stream.",e);
 302+ }
271303 }
272304 return null;
273305 }
@@ -278,9 +310,13 @@
279311 try {
280312 sin = istrm.readLine();
281313 } catch (IOException e) {
282 - log.warn("I/O problem in reading from stream",e);
 314+ if (log.isEnabledFor(Level.WARN)){
 315+ log.warn("I/O problem in reading from stream",e);
 316+ }
283317 }
284 - log.debug("<<<"+ sin);
 318+ if (log.isEnabledFor(Level.DEBUG)){
 319+ log.debug("<<<"+ sin);
 320+ }
285321 return sin;
286322 }
287323

Status & tagging log