spring-webfluxreactor-nettyaccess-log

Webflux access log header


How to customize the reactor access log in Spring webflux?

I am able to turn on reactor netty access log by setting -Dreactor.netty.http.server.accessLogEnabled=true

I would like to customize the format, eg: I need a few request headers to be logged and remove the IP address.

Any hints to achieve this in Spring Webflux application would be helpful.


Solution

  • You can do it programmatically like this

        @Component
    public class MyNettyWebServerCustomizer
            implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {
        @Override
        public void customize(NettyReactiveWebServerFactory factory) {
            factory.addServerCustomizers(httpServer -> httpServer.accessLog(true, x -> AccessLog.create("method={}, uri={}", x.method(), x.uri())));
        }
    }
    

    More about custom access logging you can find in the documentation