-
Notifications
You must be signed in to change notification settings - Fork 0
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
fix: [P4PU-687] Added feign custom logger #158
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
436320f
P4PU-687 added log level debug to connector
Giuseppe-LaManna 221679a
P4PU-687 added custom feign client logger
Giuseppe-LaManna b8e888a
P4PU-687 updated CustomFeignClientLogger and feignClient
Giuseppe-LaManna a0504de
P4PU-687 added test
Giuseppe-LaManna 992c814
P4PU-687 updated payment notices
Giuseppe-LaManna 111a59e
P4PU-687 updated tests
Giuseppe-LaManna fc88405
P4PU-687 updated CustomFeignClientLogger
Giuseppe-LaManna 36e3368
P4PU-687 added test
Giuseppe-LaManna 35549ac
P4PU-687 updated var prefix
Giuseppe-LaManna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/main/java/it/gov/pagopa/arc/config/CustomFeignClientLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package it.gov.pagopa.arc.config; | ||
|
||
import feign.Logger; | ||
import feign.Request; | ||
import feign.Response; | ||
import feign.Util; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
@Slf4j | ||
public class CustomFeignClientLogger extends Logger { | ||
|
||
@Override | ||
protected void logRequest(String configKey, Level logLevel, Request request) { | ||
/* | ||
* This method is intentionally left blank to prevent it | ||
* from logging the request. We will log both the request and the response | ||
* in the logAndRebufferResponse method. | ||
*/ | ||
} | ||
|
||
@Override | ||
protected Response logAndRebufferResponse( | ||
String configKey, Level logLevel, Response response, long elapsedTime) throws IOException { | ||
int status = response.status(); | ||
Request request = response.request(); | ||
|
||
log(configKey,"[FEIGN_CLIENT_REQUEST] ---> %s %s", request.httpMethod(), request.url()); | ||
|
||
if(status >= HttpStatus.BAD_REQUEST.value() && response.body() != null){ | ||
byte[] bodyData = Util.toByteArray(response.body().asInputStream()); | ||
if (bodyData.length > 0) { | ||
String responseString = new String(bodyData, StandardCharsets.UTF_8); | ||
|
||
log(configKey,"[FEIGN_CLIENT_RESPONSE] <--- Status: %d, response reason: %s, elapsed time (%d ms), response: %s", status, response.reason(), elapsedTime, responseString); | ||
} | ||
}else{ | ||
log(configKey,"[FEIGN_CLIENT_RESPONSE] <--- Status: %d, response reason: %s, elapsed time (%d ms)", status, response.reason(), elapsedTime); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
@Override | ||
protected void log(String configKey, String format, Object... args) { | ||
log.info(String.format(methodTag(configKey).concat(format), args)); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/it/gov/pagopa/arc/config/FeignLoggingConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package it.gov.pagopa.arc.config; | ||
|
||
import feign.Logger; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class FeignLoggingConfig { | ||
@Bean | ||
Logger feignLogger() { | ||
return new CustomFeignClientLogger(); | ||
} | ||
|
||
@Bean | ||
Logger.Level feignLoggerLevel(){ | ||
return Logger.Level.FULL; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
src/test/java/it/gov/pagopa/arc/config/CustomFeignClientLoggerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package it.gov.pagopa.arc.config; | ||
|
||
import feign.Request; | ||
import feign.Response; | ||
import feign.Util; | ||
import it.gov.pagopa.arc.utils.MemoryAppender; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class CustomFeignClientLoggerTest { | ||
private static final String CONFIG_KEY = "ExampleClass#exampleMethod(String p)"; | ||
private final CustomFeignClientLogger customFeignClientLogger = new CustomFeignClientLogger(); | ||
private Request request; | ||
private MemoryAppender memoryAppender; | ||
@BeforeEach | ||
void setUp() { | ||
request = Request.create( | ||
Request.HttpMethod.GET, | ||
"https://api.example.com/test", | ||
new HashMap<>(), | ||
null, | ||
StandardCharsets.UTF_8, | ||
null | ||
); | ||
|
||
ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("it.gov.pagopa.arc.config.CustomFeignClientLogger"); | ||
memoryAppender = new MemoryAppender(); | ||
logger.setLevel(ch.qos.logback.classic.Level.INFO); | ||
logger.addAppender(memoryAppender); | ||
memoryAppender.start(); | ||
} | ||
|
||
@Test | ||
void givenRequestWhenLogRequestThenLogNothing() { | ||
//given | ||
//when | ||
customFeignClientLogger.logRequest(CONFIG_KEY, feign.Logger.Level.FULL, request); | ||
//then | ||
assertEquals(0, memoryAppender.getLoggedEvents().size()); | ||
|
||
} | ||
|
||
@Test | ||
void givenResponseWhenLogAndRebufferResponseThenLog() throws IOException { | ||
//given | ||
String responseBody = "{\"notices\": []}"; | ||
Response response = Response.builder() | ||
.status(200) | ||
.reason("OK") | ||
.request(request) | ||
.body(responseBody, StandardCharsets.UTF_8) | ||
.build(); | ||
//when | ||
Response clonedResponse = customFeignClientLogger.logAndRebufferResponse(CONFIG_KEY, feign.Logger.Level.FULL, response, 100); | ||
//then | ||
byte[] bodyData = Util.toByteArray(clonedResponse.body().asInputStream()); | ||
String responseString = new String(bodyData, StandardCharsets.UTF_8); | ||
|
||
assertEquals(2, memoryAppender.getLoggedEvents().size()); | ||
assertEquals(responseBody, responseString); | ||
Assertions.assertTrue(memoryAppender.getLoggedEvents().get(0).getFormattedMessage().contains("[ExampleClass#exampleMethod] [FEIGN_CLIENT_REQUEST] ---> GET https://api.example.com/test")); | ||
Assertions.assertTrue(memoryAppender.getLoggedEvents().get(1).getFormattedMessage().contains("[ExampleClass#exampleMethod] [FEIGN_CLIENT_RESPONSE] <--- Status: 200, response reason: OK, elapsed time (100 ms)")); | ||
} | ||
|
||
@Test | ||
void givenErrorResponseWhenLogAndRebufferResponseThenLogError() throws IOException { | ||
//given | ||
String responseBody = "{\"error\":\"No records found for the requested user\"}"; | ||
Response response = Response.builder() | ||
.status(404) | ||
.reason("Not Found") | ||
.request(request) | ||
.body(responseBody, StandardCharsets.UTF_8) | ||
.build(); | ||
//when | ||
Response clonedResponse = customFeignClientLogger.logAndRebufferResponse(CONFIG_KEY, feign.Logger.Level.FULL, response, 100); | ||
//then | ||
byte[] bodyData = Util.toByteArray(clonedResponse.body().asInputStream()); | ||
String responseString = new String(bodyData, StandardCharsets.UTF_8); | ||
|
||
assertEquals(2, memoryAppender.getLoggedEvents().size()); | ||
assertEquals(responseBody, responseString); | ||
Assertions.assertTrue(memoryAppender.getLoggedEvents().get(0).getFormattedMessage().contains("[ExampleClass#exampleMethod] [FEIGN_CLIENT_REQUEST] ---> GET https://api.example.com/test")); | ||
Assertions.assertTrue(memoryAppender.getLoggedEvents().get(1).getFormattedMessage().contains("[ExampleClass#exampleMethod] [FEIGN_CLIENT_RESPONSE] <--- Status: 404, response reason: Not Found, elapsed time (100 ms), response: {\"error\":\"No records found for the requested user\"}")); | ||
} | ||
|
||
@Test | ||
void givenEmptyBodyWhenLogAndRebufferResponseThenLog() throws IOException { | ||
//given | ||
Response response = Response.builder() | ||
.status(200) | ||
.reason("OK") | ||
.request(request) | ||
.build(); | ||
//when | ||
Response clonedResponse = customFeignClientLogger.logAndRebufferResponse(CONFIG_KEY, feign.Logger.Level.FULL, response, 100); | ||
//then | ||
|
||
assertEquals(2, memoryAppender.getLoggedEvents().size()); | ||
|
||
assertNull(clonedResponse.body()); | ||
Assertions.assertTrue(memoryAppender.getLoggedEvents().get(0).getFormattedMessage().contains("[ExampleClass#exampleMethod] [FEIGN_CLIENT_REQUEST] ---> GET https://api.example.com/test")); | ||
Assertions.assertTrue(memoryAppender.getLoggedEvents().get(1).getFormattedMessage().contains("[ExampleClass#exampleMethod] [FEIGN_CLIENT_RESPONSE] <--- Status: 200, response reason: OK, elapsed time (100 ms)")); | ||
} | ||
|
||
@Test | ||
void givenEmptyBodyAnd400ErrorWhenLogAndRebufferResponseThenLog() throws IOException { | ||
//given | ||
Response response = Response.builder() | ||
.status(400) | ||
.reason("BAD REQUEST") | ||
.request(request) | ||
.build(); | ||
//when | ||
Response clonedResponse = customFeignClientLogger.logAndRebufferResponse(CONFIG_KEY, feign.Logger.Level.FULL, response, 100); | ||
//then | ||
|
||
assertEquals(2, memoryAppender.getLoggedEvents().size()); | ||
|
||
assertNull(clonedResponse.body()); | ||
Assertions.assertTrue(memoryAppender.getLoggedEvents().get(0).getFormattedMessage().contains("[ExampleClass#exampleMethod] [FEIGN_CLIENT_REQUEST] ---> GET https://api.example.com/test")); | ||
Assertions.assertTrue(memoryAppender.getLoggedEvents().get(1).getFormattedMessage().contains("[ExampleClass#exampleMethod] [FEIGN_CLIENT_RESPONSE] <--- Status: 400, response reason: BAD REQUEST, elapsed time (100 ms)")); | ||
} | ||
|
||
@Test | ||
void givenConfigKeyAndFormatStringWhenLogThenReturnLogInfo() { | ||
//given | ||
String formatString = "This is an example string %s"; | ||
//when | ||
customFeignClientLogger.log(CONFIG_KEY, formatString, "Arg1"); | ||
//then | ||
assertEquals(1, memoryAppender.getLoggedEvents().size()); | ||
Assertions.assertTrue(memoryAppender.getLoggedEvents().get(0).getFormattedMessage().contains("[ExampleClass#exampleMethod] This is an example string Arg1")); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
for logging levels, let's use a common prefix