Java log request headers

Logging Spring WebClient Calls

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

We rely on other people’s code in our own work. Every day.

It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production — debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky.

Lightrun is a new kind of debugger.

It’s one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics.

Learn more in this quick, 5-minute Lightrun tutorial:

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Читайте также:  Html5book ru html tags

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.

The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

We’re looking for a new Java technical editor to help review new articles for the site.

1. Overview

In this tutorial, we are going to show how to customize Spring’s WebClient – a reactive HTTP client – to log requests and responses.

2. WebClient

WebClient is a reactive and non-blocking interface for HTTP requests, based on Spring WebFlux. It has a functional, fluent API with reactive types for declarative composition.

Behind the scenes, WebClient calls an HTTP client. Reactor Netty is the default and reactive HttpClient of Jetty is also supported. Moreover, it’s possible to plug other implementations of HTTP client by setting up a ClientConnector for WebClient.

3. Logging Requests and Responses

The default HttpClient used by WebClient is the Netty implementation, so after we change the reactor.netty.http.client logging level to DEBUG, we can see some request logging, but if we need a customized log, we can configure our loggers via WebClient#filters:

WebClient .builder() .filters(exchangeFilterFunctions -> < exchangeFilterFunctions.add(logRequest()); exchangeFilterFunctions.add(logResponse()); >) .build()

In this code snippet, we’ve added two separate filters to log the request and the response.

Читайте также:  Java sql date and java util date

Let’s implement logRequest by using ExchangeFilterFunction#ofRequestProcessor:

ExchangeFilterFunction logRequest() < return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> < if (log.isDebugEnabled()) < StringBuilder sb = new StringBuilder("Request: \n"); //append clientRequest method and url clientRequest .headers() .forEach((name, values) ->values.forEach(value -> /* append header key/value */)); log.debug(sb.toString()); > return Mono.just(clientRequest); >); >

logResponse is the same, but we have to use ExchangeFilterFunction#ofResponseProcessor instead.

Now we can change the reactor.netty.http.client log level to INFO or ERROR to have a cleaner output.

4. Logging Request and Response with Body

HTTP clients have features to log the bodies of requests and responses. Thus, to achieve the goal, we are going to use a log-enabled HTTP client with our WebClient.

We can do this by manually setting WebClient.Builder#clientConnector – let’s see with Jetty and Netty HTTP clients.

4.1. Logging with Jetty HttpClient

First, let’s add the Maven dependency for jetty-reactive-httpclient to our pom:

 org.eclipse.jetty jetty-reactive-httpclient 1.1.6 

Then we’re going to create a customized Jetty HttpClient:

SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(); HttpClient httpClient = new HttpClient(sslContextFactory) < @Override public Request newRequest(URI uri) < Request request = super.newRequest(uri); return enhance(request); >>;

Here, we’ve overridden HttpClient#newRequest, then wrapped the Request in a log enhancer.

Next, we need to register events with the request so that we can log as each part of the request becomes available:

Request enhance(Request request) < StringBuilder group = new StringBuilder(); request.onRequestBegin(theRequest ->< // append request url and method to group >); request.onRequestHeaders(theRequest -> < for (HttpField header : theRequest.getHeaders()) < // append request headers to group >>); request.onRequestContent((theRequest, content) -> < // append content to group >); request.onRequestSuccess(theRequest -> < log.debug(group.toString()); group.delete(0, group.length()); >); group.append("\n"); request.onResponseBegin(theResponse -> < // append response status to group >); request.onResponseHeaders(theResponse -> < for (HttpField header : theResponse.getHeaders()) < // append response headers to group >>); request.onResponseContent((theResponse, content) -> < // append content to group >); request.onResponseSuccess(theResponse -> < log.debug(group.toString()); >); return request; >

Finally, we have to build the WebClient instance:

WebClient .builder() .clientConnector(new JettyClientHttpConnector(httpClient)) .build()

Of course, as we did before, we’ll need to set the log level of RequestLogEnhancer to DEBUG.

4.2. Logging with Netty HttpClient

First, let’s create a Netty HttpClient:

HttpClient httpClient = HttpClient .create() .wiretap(true)

Having enabled the wiretap, each request and response will be logged in full detail.

Читайте также:  Background image css two images

Next, we have to set the log level of Netty’s client package reactor.netty.http.client to DEBUG:

logging.level.reactor.netty.http.client=DEBUG

Now, let’s build the WebClient:

WebClient .builder() .clientConnector(new ReactorClientHttpConnector(httpClient)) .build()

Our WebClient will log every request and response in full detail, but the default format of Netty built-in logger contains both Hex and Text representation of bodies and also a lot of data about request and response events.

So, if we need only the text logger for Netty, we can configure the HttpClient:

HttpClient httpClient = HttpClient .create() .wiretap("reactor.netty.http.client.HttpClient", LogLevel.DEBUG, AdvancedByteBufFormat.TEXTUAL);

5. Conclusion

In this tutorial, we’ve used several techniques for logging request and response data while using Spring WebClient.

As always the code is available over on GitHub.

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

Источник

Оцените статью