-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add exclude pattern to access log #16891
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created smallrye/smallrye-config#558 which should allow using |
||
|
||
/** | ||
* The access log pattern. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens if it is not a valid regex? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would like throw an exception (can't remember which one) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. anyway i think there must be a test scenario for the exception that takes place another question that came to me, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pattern.compile is part of the JDK, I am not going at add additional tests that verify the JDK is working as expected (unless we have reason to suspect there is a bug in the JDK, which is just not the case here). If this is invalid the application will fail to start. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @stuartwdouglas, Analogy: and still has the counterpoint of how a dev in the future knows that an exception in this code snippet should stop the app from starting? |
||
} else { | ||
this.excludePattern = null; | ||
} | ||
} | ||
|
||
public AccessLogHandler(final AccessLogReceiver accessLogReceiver, String formatString, final ExchangeAttribute attribute) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It makes no practical difference. |
||
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<Void>() { | ||
@Override | ||
public void handle(Void event) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there should be a other test in case of ingestion of an invalid regex.