parent
62eb0f7428
commit
52a90adf92
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-5-reactive-client</artifactId>
|
||||
<name>spring-5-reactive-client</name>
|
||||
|
|
|
@ -116,7 +116,9 @@ public class WebClientLoggingIntegrationTest {
|
|||
|
||||
reactor.netty.http.client.HttpClient httpClient = HttpClient
|
||||
.create()
|
||||
.tcpConfiguration(tcpClient -> tcpClient.bootstrap(b -> BootstrapHandlers.updateLogSupport(b, new CustomLogger(HttpClient.class))));
|
||||
.tcpConfiguration(
|
||||
tc -> tc.bootstrap(
|
||||
b -> BootstrapHandlers.updateLogSupport(b, new CustomLogger(HttpClient.class))));
|
||||
WebClient
|
||||
.builder()
|
||||
.clientConnector(new ReactorClientHttpConnector(httpClient))
|
||||
|
|
|
@ -8,26 +8,47 @@ import reactor.core.publisher.Mono;
|
|||
|
||||
@Slf4j
|
||||
public class LogFilters {
|
||||
public static List<ExchangeFilterFunction> prepareFilters(){
|
||||
return Arrays.asList(ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
|
||||
if ( log.isDebugEnabled()) {
|
||||
StringBuilder sb = new StringBuilder("Request: \n").append(clientRequest.method()).append(" ").append(clientRequest.url());
|
||||
public static List<ExchangeFilterFunction> prepareFilters() {
|
||||
return Arrays.asList(logRequest(), logResponse());
|
||||
}
|
||||
|
||||
private static ExchangeFilterFunction logRequest() {
|
||||
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
|
||||
if (log.isDebugEnabled()) {
|
||||
StringBuilder sb = new StringBuilder("Request: \n")
|
||||
.append(clientRequest.method())
|
||||
.append(" ")
|
||||
.append(clientRequest.url());
|
||||
clientRequest
|
||||
.headers()
|
||||
.forEach((name, values) -> values.forEach(value -> sb.append("\n").append(name).append(":").append(value)));
|
||||
.forEach((name, values) -> values.forEach(value -> sb
|
||||
.append("\n")
|
||||
.append(name)
|
||||
.append(":")
|
||||
.append(value)));
|
||||
log.debug(sb.toString());
|
||||
}
|
||||
return Mono.just(clientRequest);
|
||||
}), ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
|
||||
if ( log.isDebugEnabled()) {
|
||||
StringBuilder sb = new StringBuilder("Response: \n").append("Status: ").append(clientResponse.rawStatusCode());
|
||||
});
|
||||
}
|
||||
|
||||
private static ExchangeFilterFunction logResponse() {
|
||||
return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
|
||||
if (log.isDebugEnabled()) {
|
||||
StringBuilder sb = new StringBuilder("Response: \n")
|
||||
.append("Status: ")
|
||||
.append(clientResponse.rawStatusCode());
|
||||
clientResponse
|
||||
.headers()
|
||||
.asHttpHeaders()
|
||||
.forEach((key, value1) -> value1.forEach(value -> sb.append("\n").append(key).append(":").append(value)));
|
||||
.forEach((key, value1) -> value1.forEach(value -> sb
|
||||
.append("\n")
|
||||
.append(key)
|
||||
.append(":")
|
||||
.append(value)));
|
||||
log.debug(sb.toString());
|
||||
}
|
||||
return Mono.just(clientResponse);
|
||||
}));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,9 @@ public class RequestLogEnhancer {
|
|||
.append(header)
|
||||
.append("\n");
|
||||
});
|
||||
request.onRequestContent((theRequest, content) -> group.append(toString(content, getCharset(theRequest.getHeaders()))));
|
||||
request.onRequestContent((theRequest, content) -> {
|
||||
group.append(toString(content, getCharset(theRequest.getHeaders())));
|
||||
});
|
||||
request.onRequestSuccess(theRequest -> {
|
||||
log.debug(group.toString());
|
||||
group.delete(0, group.length());
|
||||
|
@ -35,13 +37,15 @@ public class RequestLogEnhancer {
|
|||
group.append("\n");
|
||||
request.onResponseBegin(theResponse -> {
|
||||
group
|
||||
.append("Response \n ")
|
||||
.append("Response \n")
|
||||
.append(theResponse.getVersion())
|
||||
.append(" ")
|
||||
.append(theResponse.getStatus());
|
||||
if (theResponse.getReason() != null) group
|
||||
.append(" ")
|
||||
.append(theResponse.getReason());
|
||||
if (theResponse.getReason() != null) {
|
||||
group
|
||||
.append(" ")
|
||||
.append(theResponse.getReason());
|
||||
}
|
||||
group.append("\n");
|
||||
});
|
||||
request.onResponseHeaders(theResponse -> {
|
||||
|
@ -50,39 +54,39 @@ public class RequestLogEnhancer {
|
|||
.append(header)
|
||||
.append("\n");
|
||||
});
|
||||
request.onResponseContent((theResponse, content) -> group.append(toString(content, getCharset(theResponse.getHeaders()))));
|
||||
request.onResponseContent((theResponse, content) -> {
|
||||
group.append(toString(content, getCharset(theResponse.getHeaders())));
|
||||
});
|
||||
request.onResponseSuccess(theResponse -> {
|
||||
log.debug(group.toString());
|
||||
});
|
||||
return request;
|
||||
}
|
||||
|
||||
|
||||
private String toString(ByteBuffer buffer, Charset charset)
|
||||
{
|
||||
private String toString(ByteBuffer buffer, Charset charset) {
|
||||
byte[] bytes;
|
||||
if (buffer.hasArray()) {
|
||||
bytes = new byte[buffer.capacity()];
|
||||
System.arraycopy(buffer.array(), 0, bytes, 0, buffer.capacity() );
|
||||
}
|
||||
else
|
||||
{
|
||||
System.arraycopy(buffer.array(), 0, bytes, 0, buffer.capacity());
|
||||
} else {
|
||||
bytes = new byte[buffer.remaining()];
|
||||
buffer.get(bytes, 0, bytes.length);
|
||||
}
|
||||
return new String(bytes, charset);
|
||||
}
|
||||
|
||||
|
||||
private Charset getCharset(HttpFields headers) {
|
||||
String contentType = headers.get(HttpHeader.CONTENT_TYPE);
|
||||
if (contentType == null) return StandardCharsets.UTF_8;
|
||||
String[] tokens = contentType
|
||||
.toLowerCase(Locale.US)
|
||||
.split("charset=");
|
||||
if (tokens.length != 2) return StandardCharsets.UTF_8;
|
||||
String encoding = tokens[1].replaceAll("[;\"]", "");
|
||||
return Charset.forName(encoding);
|
||||
if (contentType != null) {
|
||||
String[] tokens = contentType
|
||||
.toLowerCase(Locale.US)
|
||||
.split("charset=");
|
||||
if (tokens.length == 2) {
|
||||
String encoding = tokens[1].replaceAll("[;\"]", "");
|
||||
return Charset.forName(encoding);
|
||||
}
|
||||
}
|
||||
return StandardCharsets.UTF_8;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,35 +3,40 @@ package com.baeldung.reactive.logging.netty;
|
|||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.logging.LoggingHandler;
|
||||
import io.netty.util.internal.PlatformDependent;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import static io.netty.util.internal.PlatformDependent.allocateUninitializedArray;
|
||||
import static java.lang.Math.max;
|
||||
import static java.nio.charset.Charset.defaultCharset;
|
||||
|
||||
public class CustomLogger extends LoggingHandler {
|
||||
public CustomLogger(Class<?> clazz) {
|
||||
super(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String format(ChannelHandlerContext ctx, String eventName, Object arg) {
|
||||
protected String format(ChannelHandlerContext ctx, String event, Object arg) {
|
||||
if (arg instanceof ByteBuf) {
|
||||
ByteBuf msg = (ByteBuf) arg;
|
||||
return decodeString(msg, msg.readerIndex(), msg.readableBytes(), Charset.defaultCharset());
|
||||
return decode(msg, msg.readerIndex(), msg.readableBytes(), defaultCharset());
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private String decodeString(ByteBuf src, int readerIndex, int len, Charset charset) {
|
||||
if (len == 0) return "";
|
||||
byte[] array;
|
||||
int offset;
|
||||
if (src.hasArray()) {
|
||||
array = src.array();
|
||||
offset = src.arrayOffset() + readerIndex;
|
||||
} else {
|
||||
array = PlatformDependent.allocateUninitializedArray(Math.max(len, 1024));
|
||||
offset = 0;
|
||||
src.getBytes(readerIndex, array, 0, len);
|
||||
private String decode(ByteBuf src, int readerIndex, int len, Charset charset) {
|
||||
if (len != 0) {
|
||||
byte[] array;
|
||||
int offset;
|
||||
if (src.hasArray()) {
|
||||
array = src.array();
|
||||
offset = src.arrayOffset() + readerIndex;
|
||||
} else {
|
||||
array = allocateUninitializedArray(max(len, 1024));
|
||||
offset = 0;
|
||||
src.getBytes(readerIndex, array, 0, len);
|
||||
}
|
||||
return new String(array, offset, len, charset);
|
||||
}
|
||||
return new String(array, offset, len, charset);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue