Skip to content

Commit

Permalink
Merge pull request #16891 from stuartwdouglas/13377
Browse files Browse the repository at this point in the history
Add exclude pattern to access log
  • Loading branch information
stuartwdouglas authored Apr 30, 2021
2 parents 85d574e + a78c4ae commit a3dfdc9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> excludePattern;

/**
* The access log pattern.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,18 +93,26 @@ public class AccessLogHandler implements Handler<RoutingContext> {
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<String> 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) {
Expand All @@ -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<Void>() {
@Override
public void handle(Void event) {
Expand Down

0 comments on commit a3dfdc9

Please sign in to comment.