BAEL-3009: Logging Spring WebClient calls.
-Review notes applied
This commit is contained in:
parent
52a90adf92
commit
e7e2c0a8a6
|
@ -3,7 +3,6 @@ package com.baeldung.reactive.logging;
|
||||||
import ch.qos.logback.classic.spi.LoggingEvent;
|
import ch.qos.logback.classic.spi.LoggingEvent;
|
||||||
import ch.qos.logback.core.Appender;
|
import ch.qos.logback.core.Appender;
|
||||||
import com.baeldung.reactive.logging.filters.LogFilters;
|
import com.baeldung.reactive.logging.filters.LogFilters;
|
||||||
import com.baeldung.reactive.logging.jetty.RequestLogEnhancer;
|
|
||||||
import com.baeldung.reactive.logging.netty.CustomLogger;
|
import com.baeldung.reactive.logging.netty.CustomLogger;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
@ -21,6 +20,7 @@ import org.springframework.web.reactive.function.client.WebClient;
|
||||||
import reactor.netty.channel.BootstrapHandlers;
|
import reactor.netty.channel.BootstrapHandlers;
|
||||||
import reactor.netty.http.client.HttpClient;
|
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.ArgumentMatchers.argThat;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
@ -69,13 +69,13 @@ public class WebClientLoggingIntegrationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenJettyHttpClient_whenEndpointIsConsumed_thenRequestAndResponseBodyShouldBeLogged() {
|
public void givenJettyHttpClient_whenEndpointIsConsumed_thenRequestAndResponseBodyLogged() {
|
||||||
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
|
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
|
||||||
org.eclipse.jetty.client.HttpClient httpClient = new org.eclipse.jetty.client.HttpClient(sslContextFactory) {
|
org.eclipse.jetty.client.HttpClient httpClient = new org.eclipse.jetty.client.HttpClient(sslContextFactory) {
|
||||||
@Override
|
@Override
|
||||||
public Request newRequest(URI uri) {
|
public Request newRequest(URI uri) {
|
||||||
Request request = super.newRequest(uri);
|
Request request = super.newRequest(uri);
|
||||||
return new RequestLogEnhancer().enhance(request);
|
return enhance(request);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ public class WebClientLoggingIntegrationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNettyHttpClientWithWiretap_whenEndpointIsConsumed_thenRequestAndResponseBodyShouldBeLogged() {
|
public void givenNettyHttpClientWithWiretap_whenEndpointIsConsumed_thenRequestAndResponseBodyLogged() {
|
||||||
|
|
||||||
reactor.netty.http.client.HttpClient httpClient = HttpClient
|
reactor.netty.http.client.HttpClient httpClient = HttpClient
|
||||||
.create()
|
.create()
|
||||||
|
@ -108,11 +108,12 @@ public class WebClientLoggingIntegrationTest {
|
||||||
.body(BodyInserters.fromObject(post))
|
.body(BodyInserters.fromObject(post))
|
||||||
.exchange()
|
.exchange()
|
||||||
.block();
|
.block();
|
||||||
|
|
||||||
verify(nettyAppender).doAppend(argThat(argument -> (((LoggingEvent) argument).getFormattedMessage()).contains("00000300")));
|
verify(nettyAppender).doAppend(argThat(argument -> (((LoggingEvent) argument).getFormattedMessage()).contains("00000300")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNettyHttpClientWithCustomLogger_whenEndpointIsConsumed_thenRequestAndResponseBodyShouldBeLogged() {
|
public void givenNettyHttpClientWithCustomLogger_whenEndpointIsConsumed_thenRequestAndResponseBodyLogged() {
|
||||||
|
|
||||||
reactor.netty.http.client.HttpClient httpClient = HttpClient
|
reactor.netty.http.client.HttpClient httpClient = HttpClient
|
||||||
.create()
|
.create()
|
||||||
|
@ -128,11 +129,12 @@ public class WebClientLoggingIntegrationTest {
|
||||||
.body(BodyInserters.fromObject(post))
|
.body(BodyInserters.fromObject(post))
|
||||||
.exchange()
|
.exchange()
|
||||||
.block();
|
.block();
|
||||||
|
|
||||||
verify(nettyAppender).doAppend(argThat(argument -> (((LoggingEvent) argument).getFormattedMessage()).contains(sampleResponseBody)));
|
verify(nettyAppender).doAppend(argThat(argument -> (((LoggingEvent) argument).getFormattedMessage()).contains(sampleResponseBody)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDefaultHttpClientWithString_whenEndpointIsConsumed_thenRequestAndResponseLogged() {
|
public void givenDefaultHttpClientWithFilter_whenEndpointIsConsumed_thenRequestAndResponseLogged() {
|
||||||
WebClient
|
WebClient
|
||||||
.builder()
|
.builder()
|
||||||
.filters(exchangeFilterFunctions -> {
|
.filters(exchangeFilterFunctions -> {
|
||||||
|
|
|
@ -13,7 +13,7 @@ import org.eclipse.jetty.http.HttpHeader;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class RequestLogEnhancer {
|
public class RequestLogEnhancer {
|
||||||
|
|
||||||
public Request enhance(Request request) {
|
public static Request enhance(Request request) {
|
||||||
StringBuilder group = new StringBuilder();
|
StringBuilder group = new StringBuilder();
|
||||||
request.onRequestBegin(theRequest -> group
|
request.onRequestBegin(theRequest -> group
|
||||||
.append("Request ")
|
.append("Request ")
|
||||||
|
@ -63,7 +63,7 @@ public class RequestLogEnhancer {
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String toString(ByteBuffer buffer, Charset charset) {
|
private static String toString(ByteBuffer buffer, Charset charset) {
|
||||||
byte[] bytes;
|
byte[] bytes;
|
||||||
if (buffer.hasArray()) {
|
if (buffer.hasArray()) {
|
||||||
bytes = new byte[buffer.capacity()];
|
bytes = new byte[buffer.capacity()];
|
||||||
|
@ -75,7 +75,7 @@ public class RequestLogEnhancer {
|
||||||
return new String(bytes, charset);
|
return new String(bytes, charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Charset getCharset(HttpFields headers) {
|
private static Charset getCharset(HttpFields headers) {
|
||||||
String contentType = headers.get(HttpHeader.CONTENT_TYPE);
|
String contentType = headers.get(HttpHeader.CONTENT_TYPE);
|
||||||
if (contentType != null) {
|
if (contentType != null) {
|
||||||
String[] tokens = contentType
|
String[] tokens = contentType
|
||||||
|
@ -88,5 +88,6 @@ public class RequestLogEnhancer {
|
||||||
}
|
}
|
||||||
return StandardCharsets.UTF_8;
|
return StandardCharsets.UTF_8;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ public class CustomLogger extends LoggingHandler {
|
||||||
ByteBuf msg = (ByteBuf) arg;
|
ByteBuf msg = (ByteBuf) arg;
|
||||||
return decode(msg, msg.readerIndex(), msg.readableBytes(), defaultCharset());
|
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) {
|
private String decode(ByteBuf src, int readerIndex, int len, Charset charset) {
|
||||||
|
|
Loading…
Reference in New Issue