Skip to content

Commit

Permalink
✨ log request and response details
Browse files Browse the repository at this point in the history
  • Loading branch information
artamonovkirill committed Sep 23, 2023
1 parent 69fd52b commit 31e1758
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
<version>${jackson-databind.version}</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
Expand Down
8 changes: 7 additions & 1 deletion src/main/groovy/com/tomtom/http/HttpClient.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.tomtom.http.response.Response
import org.apache.hc.client5.http.classic.HttpClient as ApacheHttpClient
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
import static org.apache.hc.client5.http.routing.RoutingSupport.determineHost

class HttpClient {

private static Logger logger = LogManager.getLogger(HttpClient)
private static ApacheHttpClient client

final RequestBuilder builder
Expand Down Expand Up @@ -262,12 +265,15 @@ class HttpClient {

private Response performRequest(Map properties) {
def request = builder.request properties
client.execute(determineHost(request), request, response -> {
def response = client.execute(determineHost(request), request, response -> {
parser.parse(
response,
properties['expecting'] as Class,
properties['of'] as Class)
})
logger.info("Request: ${request}")
logger.info("Response: ${response}")
return response
}

}
5 changes: 4 additions & 1 deletion src/main/groovy/com/tomtom/http/ResponseParser.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ import groovy.transform.PackageScope
import org.apache.hc.core5.http.HttpEntity
import org.apache.hc.core5.http.HttpResponse
import org.apache.hc.core5.http.io.entity.EntityUtils
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger

@PackageScope
class ResponseParser {

private static Logger logger = LogManager.getLogger(HttpClient)
private mapper = new ObjectMapper()

Response parse(HttpResponse response, Class type, Class subtype) {
Expand All @@ -50,7 +53,7 @@ class ResponseParser {
try {
return mapper.readValue(content, type)
} catch (Exception e) {
println "Failed to deserialize $content to $rawType due to $e.message, defaulting to string"
logger.warn "Failed to deserialize $content to $rawType due to $e.message, defaulting to string"
}
}
content
Expand Down

0 comments on commit 31e1758

Please sign in to comment.