Logging HTTP Requests in Spring using Spring Boot Actuator

Spring boot actuator

The Spring Boot Actuator can be used for logging and tracing HTTP requests.

Dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.3.2.RELEASE</version>
</dependency>

Configuration

import org.springframework.boot.actuate.trace.http.HttpTraceRepository;
import org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpTraceActuatorConfiguration {

    @Bean
    public HttpTraceRepository httpTraceRepository() {
        return new InMemoryHttpTraceRepository();
    }

}

Logging

Run your spring boot application.

Once it is running, query the endpoint http://localhost:8080/actuator/httptrace

The endpoint actuator/httptrace will return http requests that will look similar to this:

{
    "traces": [
        {
            "timestamp": "2020-08-06T15:09:17.190582Z",
            "principal": null,
            "session": null,
            "request": {
                "method": "GET",
                "uri": "http://localhost:8080/actuator/trace",
                "headers": {
                    "postman-token": [
                        "67bac2fc-3325-4a69-9af5-7237444b9f08"
                    ],
                    "host": [
                        "localhost:8080"
                    ],
                    "connection": [
                        "keep-alive"
                    ],
                    "cache-control": [
                        "no-cache"
                    ],
                    "accept-encoding": [
                        "gzip, deflate, br"
                    ],
                    "user-agent": [
                        "PostmanRuntime/7.26.2"
                    ],
                    "accept": [
                        "*/*"
                    ]
                },
                "remoteAddress": null
            },
            "response": {
                "status": 404,
                "headers": {
                    "Vary": [
                        "Origin",
                        "Access-Control-Request-Method",
                        "Access-Control-Request-Headers"
                    ]
                }
            },
            "timeTaken": 99
        },
        {
            "timestamp": "2020-08-06T15:08:49.552773Z",
            "principal": null,
            "session": null,
            "request": {
                "method": "GET",
                "uri": "http://localhost:8080/hello",
                "headers": {
                    "postman-token": [
                        "33709c05-6106-4b1b-ad34-e9e76599982c"
                    ],
                    "host": [
                        "localhost:8080"
                    ],
                    "connection": [
                        "keep-alive"
                    ],
                    "cache-control": [
                        "no-cache"
                    ],
                    "accept-encoding": [
                        "gzip, deflate, br"
                    ],
                    "user-agent": [
                        "PostmanRuntime/7.26.2"
                    ],
                    "accept": [
                        "*/*"
                    ]
                },
                "remoteAddress": null
            },
            "response": {
                "status": 200,
                "headers": {
                    "Transfer-Encoding": [
                        "chunked"
                    ],
                    "Keep-Alive": [
                        "timeout=60"
                    ],
                    "Connection": [
                        "keep-alive"
                    ],
                    "Date": [
                        "Thu, 06 Aug 2020 15:08:51 GMT"
                    ],
                    "Content-Type": [
                        "application/json"
                    ]
                }
            },
            "timeTaken": 2892
        }
}

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.