parent
62eb0f7428
commit
52a90adf92
|
@ -116,7 +116,9 @@ public class WebClientLoggingIntegrationTest {
|
||||||
|
|
||||||
reactor.netty.http.client.HttpClient httpClient = HttpClient
|
reactor.netty.http.client.HttpClient httpClient = HttpClient
|
||||||
.create()
|
.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
|
WebClient
|
||||||
.builder()
|
.builder()
|
||||||
.clientConnector(new ReactorClientHttpConnector(httpClient))
|
.clientConnector(new ReactorClientHttpConnector(httpClient))
|
||||||
|
|
|
@ -8,26 +8,47 @@ import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class LogFilters {
|
public class LogFilters {
|
||||||
public static List<ExchangeFilterFunction> prepareFilters(){
|
public static List<ExchangeFilterFunction> prepareFilters() {
|
||||||
return Arrays.asList(ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
|
return Arrays.asList(logRequest(), logResponse());
|
||||||
if ( log.isDebugEnabled()) {
|
}
|
||||||
StringBuilder sb = new StringBuilder("Request: \n").append(clientRequest.method()).append(" ").append(clientRequest.url());
|
|
||||||
|
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
|
clientRequest
|
||||||
.headers()
|
.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());
|
log.debug(sb.toString());
|
||||||
}
|
}
|
||||||
return Mono.just(clientRequest);
|
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
|
clientResponse
|
||||||
.headers()
|
.headers()
|
||||||
.asHttpHeaders()
|
.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());
|
log.debug(sb.toString());
|
||||||
}
|
}
|
||||||
return Mono.just(clientResponse);
|
return Mono.just(clientResponse);
|
||||||
}));
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,9 @@ public class RequestLogEnhancer {
|
||||||
.append(header)
|
.append(header)
|
||||||
.append("\n");
|
.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 -> {
|
request.onRequestSuccess(theRequest -> {
|
||||||
log.debug(group.toString());
|
log.debug(group.toString());
|
||||||
group.delete(0, group.length());
|
group.delete(0, group.length());
|
||||||
|
@ -35,13 +37,15 @@ public class RequestLogEnhancer {
|
||||||
group.append("\n");
|
group.append("\n");
|
||||||
request.onResponseBegin(theResponse -> {
|
request.onResponseBegin(theResponse -> {
|
||||||
group
|
group
|
||||||
.append("Response \n ")
|
.append("Response \n")
|
||||||
.append(theResponse.getVersion())
|
.append(theResponse.getVersion())
|
||||||
.append(" ")
|
.append(" ")
|
||||||
.append(theResponse.getStatus());
|
.append(theResponse.getStatus());
|
||||||
if (theResponse.getReason() != null) group
|
if (theResponse.getReason() != null) {
|
||||||
|
group
|
||||||
.append(" ")
|
.append(" ")
|
||||||
.append(theResponse.getReason());
|
.append(theResponse.getReason());
|
||||||
|
}
|
||||||
group.append("\n");
|
group.append("\n");
|
||||||
});
|
});
|
||||||
request.onResponseHeaders(theResponse -> {
|
request.onResponseHeaders(theResponse -> {
|
||||||
|
@ -50,39 +54,39 @@ public class RequestLogEnhancer {
|
||||||
.append(header)
|
.append(header)
|
||||||
.append("\n");
|
.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 -> {
|
request.onResponseSuccess(theResponse -> {
|
||||||
log.debug(group.toString());
|
log.debug(group.toString());
|
||||||
});
|
});
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String toString(ByteBuffer buffer, Charset charset) {
|
||||||
private 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()];
|
||||||
System.arraycopy(buffer.array(), 0, bytes, 0, buffer.capacity() );
|
System.arraycopy(buffer.array(), 0, bytes, 0, buffer.capacity());
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
bytes = new byte[buffer.remaining()];
|
bytes = new byte[buffer.remaining()];
|
||||||
buffer.get(bytes, 0, bytes.length);
|
buffer.get(bytes, 0, bytes.length);
|
||||||
}
|
}
|
||||||
return new String(bytes, charset);
|
return new String(bytes, charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private Charset getCharset(HttpFields headers) {
|
private Charset getCharset(HttpFields headers) {
|
||||||
String contentType = headers.get(HttpHeader.CONTENT_TYPE);
|
String contentType = headers.get(HttpHeader.CONTENT_TYPE);
|
||||||
if (contentType == null) return StandardCharsets.UTF_8;
|
if (contentType != null) {
|
||||||
String[] tokens = contentType
|
String[] tokens = contentType
|
||||||
.toLowerCase(Locale.US)
|
.toLowerCase(Locale.US)
|
||||||
.split("charset=");
|
.split("charset=");
|
||||||
if (tokens.length != 2) return StandardCharsets.UTF_8;
|
if (tokens.length == 2) {
|
||||||
String encoding = tokens[1].replaceAll("[;\"]", "");
|
String encoding = tokens[1].replaceAll("[;\"]", "");
|
||||||
return Charset.forName(encoding);
|
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.buffer.ByteBuf;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.handler.logging.LoggingHandler;
|
import io.netty.handler.logging.LoggingHandler;
|
||||||
import io.netty.util.internal.PlatformDependent;
|
|
||||||
import java.nio.charset.Charset;
|
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 class CustomLogger extends LoggingHandler {
|
||||||
public CustomLogger(Class<?> clazz) {
|
public CustomLogger(Class<?> clazz) {
|
||||||
super(clazz);
|
super(clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String format(ChannelHandlerContext ctx, String eventName, Object arg) {
|
protected String format(ChannelHandlerContext ctx, String event, Object arg) {
|
||||||
if (arg instanceof ByteBuf) {
|
if (arg instanceof ByteBuf) {
|
||||||
ByteBuf msg = (ByteBuf) arg;
|
ByteBuf msg = (ByteBuf) arg;
|
||||||
return decodeString(msg, msg.readerIndex(), msg.readableBytes(), Charset.defaultCharset());
|
return decode(msg, msg.readerIndex(), msg.readableBytes(), defaultCharset());
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
private String decodeString(ByteBuf src, int readerIndex, int len, Charset charset) {
|
private String decode(ByteBuf src, int readerIndex, int len, Charset charset) {
|
||||||
if (len == 0) return "";
|
if (len != 0) {
|
||||||
byte[] array;
|
byte[] array;
|
||||||
int offset;
|
int offset;
|
||||||
if (src.hasArray()) {
|
if (src.hasArray()) {
|
||||||
array = src.array();
|
array = src.array();
|
||||||
offset = src.arrayOffset() + readerIndex;
|
offset = src.arrayOffset() + readerIndex;
|
||||||
} else {
|
} else {
|
||||||
array = PlatformDependent.allocateUninitializedArray(Math.max(len, 1024));
|
array = allocateUninitializedArray(max(len, 1024));
|
||||||
offset = 0;
|
offset = 0;
|
||||||
src.getBytes(readerIndex, array, 0, len);
|
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