Skip to content

Commit

Permalink
applying suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-amiaud committed Jul 6, 2023
1 parent 03219c4 commit 547aeb4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 27 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@
/.README.md.html
/.factorypath
/.upgrade.md.html

# Intellij
.idea/
*.iml
*.iws

# Mac
.DS_Store

# Maven
log/
target/
24 changes: 9 additions & 15 deletions plume-admin-api-log/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ new OkHttpLoggerInterceptor(
)
```

Example to omit logging requests that matches URL regex `^https://test.coreoz.com/([^/]*)/world`:
```java
new OkHttpLoggerInterceptor(
"Github",
logApiService,
RequestPredicate.alwaysTrue().filterUrlRegex(List.of("^https://test.coreoz.com/([^/]*)/world"))
)
```

Example to hide certain json objet keys. Here : `contractId` and `password` values will be replace by `****` :

*Only work for key/value and not array or objects*
Expand Down Expand Up @@ -101,21 +110,6 @@ new OkHttpLoggerInterceptor(
)
```

Example to chain transformers to log custom api names, and only the first 1024 chars of the request/response, only for requests with 'Authorization' Header and that is HTTPS
```java
new OkHttpLoggerInterceptor(
"Github",
logApiService,
LogEntryTransformer.limitBodySizeTransformer(1024)
.andApply((request, response, logInterceptApiBean) -> {
String newName = logInterceptApiBean.getApiName() + "-HTTPS-1024chars";
logInterceptApiBean.setApiName(newName);
return logInterceptApiBean;
})
.applyOnlyToRequests(request -> request.isHttps() && request.header("Authorization") != null)
)
```

`LogEntryTransformer` and `RequestPredicate` are interfaces, so the possibilities of transforming and filtering are prettry much endless.

Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #transform(Request, Response, LogInterceptApiBean)}.
*
* <p>The static method {@link #emptyTransformer()} initiates the transformer
* <p>Starting from the empty transformer: {@code LogEntryTransformer.emptyTransformer()}
* <p>Starting using an existing transformer: {@code LogEntryTransformer.limitBodySizeTransformer(1000)} or {@code LogEntryTransformer.hideJsonFields(...)} or {@code LogEntryTransformer.emptyBody()}
*/
@FunctionalInterface
public interface LogEntryTransformer {
Expand Down Expand Up @@ -48,7 +49,7 @@ default LogEntryTransformer applyOnlyToResponsesWithHeader(String headerName, St
}

/**
* This static method transforms the body of either the request or the response
* Transforms the body of either the request or the response
* to be truncated to a given limit
* @param bodyCharLengthLimit : the limit of the body to be applied
* @return the corresponding {@link LogEntryTransformer}
Expand All @@ -71,7 +72,7 @@ static LogEntryTransformer limitBodySizeTransformer(int bodyCharLengthLimit) {
}

/**
* This static method transforms the body of either the request or the response
* Transforms the body of either the request or the response
* to replace values of given object keys through a regex matcher
* @param jsonFieldKeysToHide : a list of keys whose value needs to be hidden
* @param replacement : the replacement for the hidden values
Expand All @@ -85,7 +86,7 @@ static LogEntryTransformer hideJsonFields(List<String> jsonFieldKeysToHide, Stri
return LogEntryTransformer.emptyTransformer();
}

Pattern compiledRegex = Pattern.compile(RegexBuilder.regexHidingFields(jsonFieldKeysToHide));
Pattern compiledRegex = Pattern.compile(RegexBuilder.buildHidingFieldsRegex(jsonFieldKeysToHide));

return (request, response, apiLogEntry) -> {
if (!Strings.isNullOrEmpty(apiLogEntry.getBodyRequest())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import java.util.List;

public class RegexBuilder {
class RegexBuilder {
private RegexBuilder() {
// Empty constructor
}

static String regexHidingFields(List<String> keysToHide) {
static String buildHidingFieldsRegex(List<String> keysToHide) {
String regexKeys = keysToHide.stream()
.reduce("", (currentRegex, element) -> {
if (!currentRegex.isEmpty()) {
Expand All @@ -23,7 +23,7 @@ static String regexHidingFields(List<String> keysToHide) {
return "";
}

static String computeUrlRegexList(List<String> urlRegexList) {
static String buildFilterUrlsRegex(List<String> urlRegexList) {
return urlRegexList.stream()
.reduce("", (currentRegex, element) -> {
if (currentRegex.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ default RequestPredicate filterEndpointStartsWith(String endpointToFilter) {

/**
* Filters a request by its URL through a URL regex list
* @param urlRegexList : the URL regex list to be filtered
* @param urlsRegex : the URL regex list to be filtered
*/
default RequestPredicate filterUrlRegex(List<String> urlRegexList) {
Objects.requireNonNull(urlRegexList);
default RequestPredicate filterUrlRegex(List<String> urlsRegex) {
Objects.requireNonNull(urlsRegex);

if (urlRegexList.isEmpty()) {
if (urlsRegex.isEmpty()) {
return alwaysTrue();
}

Pattern compiledRegex = Pattern.compile(RegexBuilder.computeUrlRegexList(urlRegexList));
Pattern compiledRegex = Pattern.compile(RegexBuilder.buildFilterUrlsRegex(urlsRegex));

return request -> test(request)
&& OkHttpMatchers.matchRequestUrlRegex(request.url(), compiledRegex);
Expand Down

0 comments on commit 547aeb4

Please sign in to comment.