简体   繁体   English

CXF RS 漂亮 HTTP 请求/响应日志记录

[英]CXF RS pretty HTTP request/response logging

I am struggling to set up pretty HTPP logging (for requests and responses)我正在努力设置漂亮的 HTPP 日志记录(用于请求和响应)

I am using CXF REST API and I am initializing CXF using Spring Boot ( https://cxf.apache.org/docs/springboot.html ). 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换句话说,我只是在定义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.并且自动我有功能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 ): 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 {}");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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