diff --git a/vertx-web/src/main/java/io/vertx/ext/web/handler/LoggerFormatterAdvanced.java b/vertx-web/src/main/java/io/vertx/ext/web/handler/LoggerFormatterAdvanced.java new file mode 100644 index 0000000000..c7b28d3a10 --- /dev/null +++ b/vertx-web/src/main/java/io/vertx/ext/web/handler/LoggerFormatterAdvanced.java @@ -0,0 +1,47 @@ +/* + * Copyright 2014 Red Hat, Inc. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * The Apache License v2.0 is available at + * http://www.opensource.org/licenses/apache2.0.php + * + * You may elect to redistribute this code under either of these licenses. + */ +package io.vertx.ext.web.handler; + +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.core.http.HttpMethod; +import io.vertx.ext.web.RoutingContext; + +/** + * Implement to format the output of the {@link LoggerHandler} + * + * @author Oded Arbel + */ +@VertxGen +@FunctionalInterface +public interface LoggerFormatterAdvanced { + + /** + * Formats and returns the log statement + * + * @param routingContext The routing context + * @param timestamp The system time in milliseconds when the request was handled by the {@link LoggerHandler} + * @param remoteClient The remote client's host address + * @param versionFormatted The HTTP version as formatted for display (normally {@code HTTP/x.x}) + * @param method The request's HTTP method + * @param uri The request's URI + * @param status The response's HTTP status code + * @param contentLength The amount of bytes that were written in the response + * @param ms The number of milliseconds since first receiving the request + * @return The formatted string to log + */ + String format(RoutingContext routingContext, long timestamp, String remoteClient, String versionFormatted, + HttpMethod method, String uri, int status, long contentLength, long ms); +} diff --git a/vertx-web/src/main/java/io/vertx/ext/web/handler/LoggerHandler.java b/vertx-web/src/main/java/io/vertx/ext/web/handler/LoggerHandler.java index d1675c4c23..a93826587d 100644 --- a/vertx-web/src/main/java/io/vertx/ext/web/handler/LoggerHandler.java +++ b/vertx-web/src/main/java/io/vertx/ext/web/handler/LoggerHandler.java @@ -69,7 +69,7 @@ static LoggerHandler create(boolean immediate, LoggerFormat format) { * * @deprecated Superseded by {@link #customFormatter(LoggerFormatter)} * @param formatter the formatting function - * @return the formatted log string + * @return self * @throws IllegalStateException if current format is not {@link LoggerFormat#CUSTOM} */ @Deprecated @@ -80,9 +80,19 @@ static LoggerHandler create(boolean immediate, LoggerFormat format) { * Set the custom formatter to be used by the handler. * * @param formatter the formatter - * @return the formatted log string + * @return self * @throws IllegalStateException if current format is not {@link LoggerFormat#CUSTOM} */ @Fluent LoggerHandler customFormatter(LoggerFormatter formatter); + + /** + * Set the custom formatter to be used by the handler. + * + * @param formatter the formatter + * @return self + * @throws IllegalStateException if current format is not {@link LoggerFormat#CUSTOM} + */ + @Fluent + LoggerHandler customFormatter(LoggerFormatterAdvanced formatter); } diff --git a/vertx-web/src/main/java/io/vertx/ext/web/handler/impl/LoggerHandlerImpl.java b/vertx-web/src/main/java/io/vertx/ext/web/handler/impl/LoggerHandlerImpl.java index 079e14de32..e88bfe7ab8 100755 --- a/vertx-web/src/main/java/io/vertx/ext/web/handler/impl/LoggerHandlerImpl.java +++ b/vertx-web/src/main/java/io/vertx/ext/web/handler/impl/LoggerHandlerImpl.java @@ -26,6 +26,7 @@ import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.handler.LoggerFormat; import io.vertx.ext.web.handler.LoggerFormatter; +import io.vertx.ext.web.handler.LoggerFormatterAdvanced; import io.vertx.ext.web.handler.LoggerHandler; import io.vertx.ext.web.impl.Utils; @@ -57,8 +58,7 @@ public class LoggerHandlerImpl implements LoggerHandler { */ private final LoggerFormat format; - private Function customFormatter; - private LoggerFormatter logFormatter; + private LoggerFormatterAdvanced logFormatter; public LoggerHandlerImpl(boolean immediate, LoggerFormat format) { this.immediate = immediate; @@ -147,11 +147,8 @@ private void log(RoutingContext context, long timestamp, String remoteClient, Ht break; case CUSTOM: try { - if (logFormatter != null) { - message = logFormatter.format(context, (System.currentTimeMillis() - timestamp)); - } else { - message = customFormatter.apply(request); - } + message = logFormatter.format(context, timestamp, remoteClient, versionFormatted, method, uri, + status, contentLength, (System.currentTimeMillis() - timestamp)); } catch (RuntimeException e) { // if an error happens at the user side // log it instead @@ -196,7 +193,8 @@ public LoggerHandler customFormatter(Function formatt throw new IllegalStateException("Setting a formatter requires the handler to be set to CUSTOM format"); } - this.customFormatter = formatter; + this.logFormatter = (RoutingContext routingContext, long timestamp, String remoteClient, String versionFormatted, + HttpMethod method, String uri, int status, long contentLength, long ms) -> formatter.apply(routingContext.request()); return this; } @@ -207,7 +205,19 @@ public LoggerHandler customFormatter(LoggerFormatter formatter) { throw new IllegalStateException("Setting a formatter requires the handler to be set to CUSTOM format"); } + this.logFormatter = (RoutingContext routingContext, long timestamp, String remoteClient, String versionFormatted, + HttpMethod method, String uri, int status, long contentLength, long ms) -> formatter.format(routingContext, ms); + return this; + } + + @Override + public LoggerHandler customFormatter(LoggerFormatterAdvanced formatter) { + if (format != LoggerFormat.CUSTOM) { + throw new IllegalStateException("Setting a formatter requires the handler to be set to CUSTOM format"); + } + this.logFormatter = formatter; return this; } + } diff --git a/vertx-web/src/test/java/io/vertx/ext/web/handler/LoggerHandlerTest.java b/vertx-web/src/test/java/io/vertx/ext/web/handler/LoggerHandlerTest.java index 4369da3941..d132c34d1c 100644 --- a/vertx-web/src/test/java/io/vertx/ext/web/handler/LoggerHandlerTest.java +++ b/vertx-web/src/test/java/io/vertx/ext/web/handler/LoggerHandlerTest.java @@ -17,6 +17,7 @@ package io.vertx.ext.web.handler; import io.vertx.core.http.HttpMethod; +import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.WebTestBase; import org.junit.Test; @@ -82,5 +83,17 @@ private void testLogger(LoggerHandler logger) throws Exception { testRequest(HttpMethod.GET, "/somedir", 200, "OK"); } + @Test + public void testLogger6() throws Exception { + final CountDownLatch latch = new CountDownLatch(1); + LoggerHandler logger = LoggerHandler.create(true, LoggerFormat.CUSTOM).customFormatter(( + RoutingContext routingContext, long timestamp, String remoteClient, String versionFormatted, + HttpMethod method, String uri, int status, long contentLength, long ms) -> { + latch.countDown(); + return "custom log message"; + }); + testLogger(logger); + latch.await(); + } }