public void handle(Request request, Response response) throws Exception { forward(request, response); String msg = String.format( COMMON_LOG_FORMAT, request.remoteIp(), "-", currentTime(), request.method(), request.uri(), request.protocol(), response.statusCode(), contentLengthOrHyphen(response)); logger.info(msg); }
@Test public void logsEmptyStringWhenNoRefererInRequest() throws Exception { request .method(GET) .uri("/products?keyword=dogs") .addHeader(HeaderNames.USER_AGENT, "Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7)"); apacheCommonLogger.handle(request, response); response.status(OK).body("a response with a size of 28").done(); response.await(); logRecords.assertEntries( contains( "192.168.0.1 - - [27/Jun/2012:18:04:00 +0100] \"GET /products?keyword=dogs HTTP/1.1\" 200 28 \"\" \"Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7)\"")); }
@Test public void logsEmptyStringWhenNoUserAgentInRequest() throws Exception { request .method(GET) .uri("/products?keyword=dogs") .addHeader(HeaderNames.REFERER, "http://lama/wool"); apacheCommonLogger.handle(request, response); response.status(OK).body("a response with a size of 28").done(); response.await(); logRecords.assertEntries( contains( "192.168.0.1 - - [27/Jun/2012:18:04:00 +0100] \"GET /products?keyword=dogs HTTP/1.1\" 200 28 \"http://lama/wool\" \"\"")); }
@Test public void hyphenReplacesContentSizeForEmptyResponses() throws Exception { request.remoteIp("192.168.0.1").method(DELETE).uri("/logout"); apacheCommonLogger.connectTo( new Application() { public void handle(Request request, Response response) throws Exception { response.body(""); response.status(NO_CONTENT); } }); apacheCommonLogger.handle(request, response); logRecords.assertEntries(contains(containsString("\"DELETE /logout HTTP/1.1\" 204 -"))); }
@Test public void logsRequestsServedInApacheCommonLogFormat() throws Exception { request.method(GET).uri("/products?keyword=dogs"); apacheCommonLogger.connectTo( new Application() { public void handle(Request request, Response response) throws Exception { response.body("a response with a size of 28"); response.status(HttpStatus.OK); } }); apacheCommonLogger.handle(request, response); logRecords.assertEntries( contains( "192.168.0.1 - - [27/Jun/2012:18:04:00 +0100] \"GET /products?keyword=dogs HTTP/1.1\" 200 28")); }