Skip to content

Commit 893da73

Browse files
committed
optimize logging
1 parent eb9773a commit 893da73

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/main/java/robaho/net/httpserver/ExchangeImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public void sendResponseHeaders(int rCode, long contentLen)
235235

236236
/* check for response type that is not allowed to send a body */
237237
if (rCode == 101) {
238-
logger.log(Level.INFO, "switching protocols");
238+
logger.log(Level.DEBUG, () -> "switching protocols");
239239

240240
if (contentLen != 0) {
241241
String msg = "sendResponseHeaders: rCode = " + rCode
@@ -310,7 +310,9 @@ else if (http10) {
310310
write(rspHdrs, ros);
311311
this.rspContentLen = contentLen;
312312
sentHeaders = true;
313-
logger.log(Level.TRACE, "Sent headers: noContentToSend=" + noContentToSend);
313+
if(logger.isLoggable(Level.TRACE)) {
314+
logger.log(Level.TRACE, "Sent headers: noContentToSend=" + noContentToSend);
315+
}
314316
if(contentLen==0) {
315317
ros.flush();
316318
}

src/main/java/robaho/net/httpserver/ServerImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ private void closeConnection(HttpConnection conn) {
362362

363363
/* per exchange task */
364364
class Exchange implements Runnable {
365-
HttpConnection connection;
365+
final HttpConnection connection;
366366
InputStream rawin;
367367
OutputStream rawout;
368368
String protocol;
@@ -379,7 +379,7 @@ public void run() {
379379
this.rawin = connection.getInputStream();
380380
this.rawout = connection.getOutputStream();
381381

382-
logger.log(Level.TRACE, "exchange started "+connection.toString());
382+
logger.log(Level.TRACE, () -> "exchange started "+connection.toString());
383383

384384
while (true) {
385385
try {
@@ -412,7 +412,7 @@ public void run() {
412412
throw t;
413413
}
414414
}
415-
logger.log(Level.TRACE, "exchange finished "+connection.toString());
415+
logger.log(Level.TRACE, () -> "exchange finished "+connection.toString());
416416
}
417417

418418
private void runPerRequest() throws IOException {
@@ -422,7 +422,7 @@ private void runPerRequest() throws IOException {
422422

423423
connection.inRequest = false;
424424
Request req = new Request(rawin, rawout);
425-
String requestLine = req.requestLine();
425+
final String requestLine = req.requestLine();
426426
connection.inRequest = true;
427427

428428
if (requestLine == null) {
@@ -431,7 +431,7 @@ private void runPerRequest() throws IOException {
431431
closeConnection(connection);
432432
return;
433433
}
434-
logger.log(Level.DEBUG, "Exchange request line: {0}", requestLine);
434+
logger.log(Level.DEBUG, () -> "Exchange request line: "+ requestLine);
435435
int space = requestLine.indexOf(' ');
436436
if (space == -1) {
437437
reject(Code.HTTP_BAD_REQUEST,
@@ -504,7 +504,7 @@ private void runPerRequest() throws IOException {
504504
}
505505
}
506506
}
507-
logger.log(Level.INFO,"protocol "+protocol+" uri "+uri+" headers "+headers);
507+
logger.log(Level.TRACE,() -> "protocol "+protocol+" uri "+uri+" headers "+headers);
508508
String uriPath = Optional.ofNullable(uri.getPath()).orElse("/");
509509
ctx = contexts.findContext(protocol, uriPath);
510510
if (ctx == null) {

0 commit comments

Comments
 (0)