BAEL-3009: Logging Spring WebClient calls.

-Review notes applied
This commit is contained in:
maryarm 2019-09-05 21:00:26 +04:30
parent 52a90adf92
commit e7e2c0a8a6
3 changed files with 13 additions and 10 deletions

View File

@ -3,7 +3,6 @@ package com.baeldung.reactive.logging;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.Appender;
import com.baeldung.reactive.logging.filters.LogFilters;
import com.baeldung.reactive.logging.jetty.RequestLogEnhancer;
import com.baeldung.reactive.logging.netty.CustomLogger;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
@ -21,6 +20,7 @@ import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.channel.BootstrapHandlers;
import reactor.netty.http.client.HttpClient;
import static com.baeldung.reactive.logging.jetty.RequestLogEnhancer.enhance;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@ -69,13 +69,13 @@ public class WebClientLoggingIntegrationTest {
}
@Test
public void givenJettyHttpClient_whenEndpointIsConsumed_thenRequestAndResponseBodyShouldBeLogged() {
public void givenJettyHttpClient_whenEndpointIsConsumed_thenRequestAndResponseBodyLogged() {
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
org.eclipse.jetty.client.HttpClient httpClient = new org.eclipse.jetty.client.HttpClient(sslContextFactory) {
@Override
public Request newRequest(URI uri) {
Request request = super.newRequest(uri);
return new RequestLogEnhancer().enhance(request);
return enhance(request);
}
};
@ -94,7 +94,7 @@ public class WebClientLoggingIntegrationTest {
}
@Test
public void givenNettyHttpClientWithWiretap_whenEndpointIsConsumed_thenRequestAndResponseBodyShouldBeLogged() {
public void givenNettyHttpClientWithWiretap_whenEndpointIsConsumed_thenRequestAndResponseBodyLogged() {
reactor.netty.http.client.HttpClient httpClient = HttpClient
.create()
@ -108,11 +108,12 @@ public class WebClientLoggingIntegrationTest {
.body(BodyInserters.fromObject(post))
.exchange()
.block();
verify(nettyAppender).doAppend(argThat(argument -> (((LoggingEvent) argument).getFormattedMessage()).contains("00000300")));
}
@Test
public void givenNettyHttpClientWithCustomLogger_whenEndpointIsConsumed_thenRequestAndResponseBodyShouldBeLogged() {
public void givenNettyHttpClientWithCustomLogger_whenEndpointIsConsumed_thenRequestAndResponseBodyLogged() {
reactor.netty.http.client.HttpClient httpClient = HttpClient
.create()
@ -128,11 +129,12 @@ public class WebClientLoggingIntegrationTest {
.body(BodyInserters.fromObject(post))
.exchange()
.block();
verify(nettyAppender).doAppend(argThat(argument -> (((LoggingEvent) argument).getFormattedMessage()).contains(sampleResponseBody)));
}
@Test
public void givenDefaultHttpClientWithString_whenEndpointIsConsumed_thenRequestAndResponseLogged() {
public void givenDefaultHttpClientWithFilter_whenEndpointIsConsumed_thenRequestAndResponseLogged() {
WebClient
.builder()
.filters(exchangeFilterFunctions -> {

View File

@ -13,7 +13,7 @@ import org.eclipse.jetty.http.HttpHeader;
@Slf4j
public class RequestLogEnhancer {
public Request enhance(Request request) {
public static Request enhance(Request request) {
StringBuilder group = new StringBuilder();
request.onRequestBegin(theRequest -> group
.append("Request ")
@ -63,7 +63,7 @@ public class RequestLogEnhancer {
return request;
}
private String toString(ByteBuffer buffer, Charset charset) {
private static String toString(ByteBuffer buffer, Charset charset) {
byte[] bytes;
if (buffer.hasArray()) {
bytes = new byte[buffer.capacity()];
@ -75,7 +75,7 @@ public class RequestLogEnhancer {
return new String(bytes, charset);
}
private Charset getCharset(HttpFields headers) {
private static Charset getCharset(HttpFields headers) {
String contentType = headers.get(HttpHeader.CONTENT_TYPE);
if (contentType != null) {
String[] tokens = contentType
@ -88,5 +88,6 @@ public class RequestLogEnhancer {
}
return StandardCharsets.UTF_8;
}
}

View File

@ -20,7 +20,7 @@ public class CustomLogger extends LoggingHandler {
ByteBuf msg = (ByteBuf) arg;
return decode(msg, msg.readerIndex(), msg.readableBytes(), defaultCharset());
}
return "";
return super.format(ctx, event, arg);
}
private String decode(ByteBuf src, int readerIndex, int len, Charset charset) {