Skip to content

Commit

Permalink
Provide a way to exclude headers from logs (OpenFeign#1530)
Browse files Browse the repository at this point in the history
* Provide a way to exclude headers from logs

* Make header filters optional (nullable), update README

* Mistyping: sing -> sign

* New implementation with overriding of filter methods

* Fix javadocs, update README

* Update README, fix removed newline
  • Loading branch information
vitalijr2 authored Oct 27, 2021
1 parent 3491dcb commit e8fa87c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,8 @@ public class Example {
The SLF4JLogger (see above) may also be of interest.
To filter out sensitive information like authorization or tokens
override methods `shouldLogRequestHeader` or `shouldLogResponseHeader`.
#### Request Interceptors
When you need to change all requests, regardless of their target, you'll want to configure a `RequestInterceptor`.
Expand Down
37 changes: 30 additions & 7 deletions core/src/main/java/feign/Logger.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -27,8 +27,7 @@
public abstract class Logger {

protected static String methodTag(String configKey) {
return new StringBuilder().append('[').append(configKey.substring(0, configKey.indexOf('(')))
.append("] ").toString();
return '[' + configKey.substring(0, configKey.indexOf('(')) + "] ";
}

/**
Expand All @@ -41,13 +40,35 @@ protected static String methodTag(String configKey) {
*/
protected abstract void log(String configKey, String format, Object... args);

/**
* Override to filter out request headers.
*
* @param header header name
* @return true to log a request header
*/
protected boolean shouldLogRequestHeader(String header) {
return true;
}

/**
* Override to filter out response headers.
*
* @param header header name
* @return true to log a response header
*/
protected boolean shouldLogResponseHeader(String header) {
return true;
}

protected void logRequest(String configKey, Level logLevel, Request request) {
log(configKey, "---> %s %s HTTP/1.1", request.httpMethod().name(), request.url());
if (logLevel.ordinal() >= Level.HEADERS.ordinal()) {

for (String field : request.headers().keySet()) {
for (String value : valuesOrEmpty(request.headers(), field)) {
log(configKey, "%s: %s", field, value);
if (shouldLogRequestHeader(field)) {
for (String value : valuesOrEmpty(request.headers(), field)) {
log(configKey, "%s: %s", field, value);
}
}
}

Expand Down Expand Up @@ -84,8 +105,10 @@ protected Response logAndRebufferResponse(String configKey,
if (logLevel.ordinal() >= Level.HEADERS.ordinal()) {

for (String field : response.headers().keySet()) {
for (String value : valuesOrEmpty(response.headers(), field)) {
log(configKey, "%s: %s", field, value);
if (shouldLogResponseHeader(field)) {
for (String value : valuesOrEmpty(response.headers(), field)) {
log(configKey, "%s: %s", field, value);
}
}
}

Expand Down
19 changes: 16 additions & 3 deletions core/src/test/java/feign/LoggerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -49,7 +49,7 @@ public class LoggerTest {
interface SendsStuff {

@RequestLine("POST /")
@Headers("Content-Type: application/json")
@Headers({"Content-Type: application/json", "X-Token: qwerty"})
@Body("%7B\"customer_name\": \"{customer_name}\", \"user_name\": \"{user_name}\", \"password\": \"{password}\"%7D")
String login(
@Param("customer_name") String customer,
Expand Down Expand Up @@ -99,7 +99,7 @@ public static Iterable<Object[]> data() {

@Test
public void levelEmits() {
server.enqueue(new MockResponse().setBody("foo"));
server.enqueue(new MockResponse().setHeader("Y-Powered-By", "Mock").setBody("foo"));

SendsStuff api = Feign.builder()
.logger(logger)
Expand Down Expand Up @@ -383,9 +383,22 @@ public Retryer clone() {

private static final class RecordingLogger extends Logger implements TestRule {

private static final String PREFIX_X = "x-";
private static final String PREFIX_Y = "y-";

private final List<String> messages = new ArrayList<>();
private final List<String> expectedMessages = new ArrayList<>();

@Override
protected boolean shouldLogRequestHeader(String header) {
return !header.toLowerCase().startsWith(PREFIX_X);
}

@Override
protected boolean shouldLogResponseHeader(String header) {
return !header.toLowerCase().startsWith(PREFIX_Y);
}

void expectMessages(List<String> expectedMessages) {
this.expectedMessages.addAll(expectedMessages);
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/java/feign/MultipleLoggerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -41,7 +41,7 @@ public void testAppendSeveralFilesToOneJavaLogger() throws Exception {
}

@Test
public void testJavaLoggerInstantationWithLoggerName() throws Exception {
public void testJavaLoggerInstantiationWithLoggerName() throws Exception {
Logger.JavaLogger l1 = new Logger.JavaLogger("First client")
.appendToFile(tmp.newFile("1.log").getAbsolutePath());
Logger.JavaLogger l2 = new Logger.JavaLogger("Second client")
Expand Down
2 changes: 1 addition & 1 deletion slf4j/src/main/java/feign/slf4j/Slf4jLogger.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down

0 comments on commit e8fa87c

Please sign in to comment.