Tried all answers, but some of them don't fit my needs or just do not work.
Wrote my own solution based on this answers to intercept request/response bodies and log them.
@Slf4j
@Component
public class LoggingCustomizer implements WebClientCustomizer {
@Override public void customize(WebClient.Builder webClientBuilder) {
webClientBuilder.filter((request, next) -> {
logRequest(request);
return next
.exchange(interceptBody(request))
.doOnNext(this::logResponse)
.map(this::interceptBody);
});
}
private ClientRequest interceptBody(ClientRequest request) {
return ClientRequest.from(request)
.body((outputMessage, context) -> request.body().insert(new ClientHttpRequestDecorator(outputMessage) {
@Override public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
return super.writeWith(Mono.from(body)
.doOnNext(dataBuffer -> logRequestBody(dataBuffer)));
}
}, context))
.build();
}
private ClientResponse interceptBody(ClientResponse response) {
return response.mutate()
.body(data -> data.doOnNext(this::logResponseBody))
.build();
}
private void logRequest(ClientRequest request) {
log.debug("DOWNSTREAM REQUEST: METHOD {}, URI: {}, HEADERS: {}", request.method(), request.url(), request.headers());
}
private void logRequestBody(DataBuffer dataBuffer) {
log.debug("DOWNSTREAM REQUEST: BODY: {}", dataBuffer.toString(StandardCharsets.UTF_8));
}
private void logResponse(ClientResponse response) {
log.debug("DOWNSTREAM RESPONSE: STATUS: {}, HEADERS: {}", response.rawStatusCode(), response.headers().asHttpHeaders());
}
private void logResponseBody(DataBuffer dataBuffer) {
log.debug("DOWNSTREAM RESPONSE: BODY: {}", dataBuffer.toString(StandardCharsets.UTF_8));
}
}
Update: added snippet to log using reactor.netty.http.client.HttpClient
(preferrable solution)
@Slf4j
@Component
public class LoggingCustomizer implements WebClientCustomizer {
@Override public void customize(WebClient.Builder webClientBuilder) {
HttpClient httpClient = HttpClient.create()
.doOnRequest((httpClientRequest, connection) -> connection.addHandlerFirst(new LoggingHandler()));
webClientBuilder.clientConnector(new ReactorClientHttpConnector(httpClient));
}
private static class LoggingHandler extends ChannelDuplexHandler {
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof FullHttpRequest request) {
log.debug("DOWNSTREAM REQUEST: METHOD: {}, URI: {}, BODY: {}, HEADERS: {}",
request.method(), request.uri(), request.content().toString(defaultCharset()), request.headers());
} else if (msg instanceof HttpRequest request) {
log.debug("DOWNSTREAM REQUEST: METHOD: {}, URI: {}, HEADERS: {}",
request.method(), request.uri(), request.headers());
} else if (msg instanceof FullHttpMessage message) {
log.debug("DOWNSTREAM REQUEST: BODY: {}",
message.content().toString(defaultCharset()));
}
super.write(ctx, msg, promise);
}
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpResponse response) {
log.debug("DOWNSTREAM RESPONSE: STATUS: {}, BODY: {}, HEADERS: {}",
response.status().code(), response.content().toString(defaultCharset()), response.headers());
} else if (msg instanceof HttpResponse response) {
log.debug("DOWNSTREAM RESPONSE: STATUS: {}, HEADERS: {}",
response.status().code(), response.headers());
} else if (!(msg instanceof LastHttpContent) && msg instanceof HttpContent httpContent) {
log.debug("DOWNSTREAM RESPONSE: BODY: {}",
httpContent.content().toString(defaultCharset()));
}
super.channelRead(ctx, msg);
}
}
}