diff --git a/extensions/vertx-http/deployment/src/test/java/io/quarkus/vertx/http/accesslog/AccessLogFileTestCase.java b/extensions/vertx-http/deployment/src/test/java/io/quarkus/vertx/http/accesslog/AccessLogFileTestCase.java index 978ed572b4f37..ecc8492fed7ee 100644 --- a/extensions/vertx-http/deployment/src/test/java/io/quarkus/vertx/http/accesslog/AccessLogFileTestCase.java +++ b/extensions/vertx-http/deployment/src/test/java/io/quarkus/vertx/http/accesslog/AccessLogFileTestCase.java @@ -70,6 +70,7 @@ public JavaArchive get() { p.setProperty("quarkus.http.access-log.base-file-name", "server"); p.setProperty("quarkus.http.access-log.log-directory", logDirectory.toAbsolutePath().toString()); p.setProperty("quarkus.http.access-log.pattern", "long"); + p.setProperty("quarkus.http.access-log.exclude-pattern", "/health|/liveliness"); ByteArrayOutputStream out = new ByteArrayOutputStream(); p.store(out, null); @@ -104,6 +105,8 @@ public void testSingleLogMessageToFile() throws IOException, InterruptedExceptio new HttpClientConfig().setParam(CoreProtocolPNames.PROTOCOL_VERSION, new ProtocolVersion("HTTP", 1, 0))); final RequestSpecification requestSpec = new RequestSpecBuilder().setConfig(http10Config).build(); final String paramValue = UUID.randomUUID().toString(); + RestAssured.given(requestSpec).get("/health"); //should be ignored + RestAssured.given(requestSpec).get("/liveliness"); //should be ignored RestAssured.given(requestSpec).get("/does-not-exist?foo=" + paramValue); Awaitility.given().pollInterval(100, TimeUnit.MILLISECONDS) @@ -117,6 +120,8 @@ public void run() throws Throwable { Path path = logDirectory.resolve("server.log"); Assertions.assertTrue(Files.exists(path)); String data = new String(Files.readAllBytes(path), StandardCharsets.UTF_8); + Assertions.assertFalse(data.contains("/health")); + Assertions.assertFalse(data.contains("/liveliness")); Assertions.assertTrue(data.contains("/does-not-exist")); Assertions.assertTrue(data.contains("?foo=" + paramValue), "access log is missing query params"); diff --git a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/AccessLogConfig.java b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/AccessLogConfig.java index 1bc16a316aaac..5b54ecc17c90d 100644 --- a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/AccessLogConfig.java +++ b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/AccessLogConfig.java @@ -14,6 +14,12 @@ public class AccessLogConfig { @ConfigItem(defaultValue = "false") public boolean enabled; + /** + * A regular expression that can be used to exclude some paths from logging. + */ + @ConfigItem + Optional excludePattern; + /** * The access log pattern. * diff --git a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/VertxHttpRecorder.java b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/VertxHttpRecorder.java index 8f532cbb00b65..34325801a3d4e 100644 --- a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/VertxHttpRecorder.java +++ b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/VertxHttpRecorder.java @@ -399,7 +399,8 @@ public void handle(HttpServerRequest event) { } else { receiver = new JBossLoggingAccessLogReceiver(accessLog.category); } - AccessLogHandler handler = new AccessLogHandler(receiver, accessLog.pattern, getClass().getClassLoader()); + AccessLogHandler handler = new AccessLogHandler(receiver, accessLog.pattern, getClass().getClassLoader(), + accessLog.excludePattern); httpRouteRouter.route().order(Integer.MIN_VALUE).handler(handler); quarkusWrapperNeeded = true; } diff --git a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/filters/accesslog/AccessLogHandler.java b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/filters/accesslog/AccessLogHandler.java index bc08866e85839..27647db90de3e 100644 --- a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/filters/accesslog/AccessLogHandler.java +++ b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/filters/accesslog/AccessLogHandler.java @@ -19,7 +19,10 @@ package io.quarkus.vertx.http.runtime.filters.accesslog; import java.util.Collections; +import java.util.Optional; import java.util.StringJoiner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import io.quarkus.vertx.http.runtime.attribute.ExchangeAttribute; import io.quarkus.vertx.http.runtime.attribute.ExchangeAttributeParser; @@ -90,18 +93,26 @@ public class AccessLogHandler implements Handler { private final AccessLogReceiver accessLogReceiver; private final String formatString; private final ExchangeAttribute tokens; + private final Pattern excludePattern; - public AccessLogHandler(final AccessLogReceiver accessLogReceiver, final String formatString, ClassLoader classLoader) { + public AccessLogHandler(final AccessLogReceiver accessLogReceiver, final String formatString, ClassLoader classLoader, + Optional excludePattern) { this.accessLogReceiver = accessLogReceiver; this.formatString = handleCommonNames(formatString); this.tokens = new ExchangeAttributeParser(classLoader, Collections.singletonList(new SubstituteEmptyWrapper("-"))) .parse(this.formatString); + if (excludePattern.isPresent()) { + this.excludePattern = Pattern.compile(excludePattern.get()); + } else { + this.excludePattern = null; + } } public AccessLogHandler(final AccessLogReceiver accessLogReceiver, String formatString, final ExchangeAttribute attribute) { this.accessLogReceiver = accessLogReceiver; this.formatString = handleCommonNames(formatString); this.tokens = attribute; + this.excludePattern = null; } private static String handleCommonNames(String formatString) { @@ -122,6 +133,13 @@ private static String handleCommonNames(String formatString) { @Override public void handle(RoutingContext rc) { + if (excludePattern != null) { + Matcher m = excludePattern.matcher(rc.request().path()); + if (m.matches()) { + rc.next(); + return; + } + } QuarkusRequestWrapper.get(rc.request()).addRequestDoneHandler(new Handler() { @Override public void handle(Void event) {