简体   繁体   中英

CXF RS pretty HTTP request/response logging

I am struggling to set up pretty HTPP logging (for requests and responses)

I am using CXF REST API and I am initializing CXF using Spring Boot ( https://cxf.apache.org/docs/springboot.html ). In other words I am just defining application.properties

server.port=8443
server.servlet.contextPath=/api/
cxf.path=/cxf
cxf.jaxrs.classes-scan=true
cxf.jaxrs.classes-scan-packages=com.mycomp \
  ,io.swagger.v3.jaxrs2.integration.resources  \
  ,com.fasterxml.jackson

and automagically I have functional REST API.

I do not want to use XML configuration, but I believe, that XML config would be (based on doc https://cxf.apache.org/docs/features.html ):

    <cxf:bus>
        <cxf:features>
            <cxf:logging/>
        </cxf:features>
    </cxf:bus>

How can I set up pretty logging programmatically?

In order to have pretty logging you need to do following:

@Configuration
/**
 * Configure CXF http requests for pretty logging
 */
public class CxfLoggingConfig {

    private static final Logger log = LoggerFactory.getLogger(CxfLoggingConfig.class);

    @Autowired
    private Bus bus;

    @PostConstruct
    private void init() {
        log.debug("Initialising CxfLoggingConfig");
        LoggingFeature loggingFeature = new LoggingFeature();
        loggingFeature.setPrettyLogging(true);
        loggingFeature.setVerbose(true);
        //loggingFeature.setLogMultipart(true);
        bus.getFeatures().add(loggingFeature);
        log.debug("CxfLoggingConfig initialised by {}");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM