如何配置日志记录以记录CXF入站和出站的restful消息?

时间:2015-11-17 15:04:04

标签: java spring apache web-services logging

我在使用log4j创建日志文件而不是java.util.logging中创建了Restful Web Service。但它没有为入站和出站消息写入日志文件。

消息记录配置下面有

我创建了 META-INF / cxf / org.apache.cxf.Logger 文件。然后我把它放在配置线下面。

org.apache.cxf.common.logging.Log4jLogger

然后我创建了 WEB-INF / classes / log4j.properties 配置文件。下面是 log4j 配置

# Root logger option
log4j.rootLogger=INFO, file, stdout


# Direct log messages to a log file

    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.File=D:\\wslog\\log.txt
    log4j.appender.file.MaxFileSize=1MB
    log4j.appender.file.MaxBackupIndex=1
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

下面的配置,以启用cxf.xml中的消息记录

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:cxf="http://cxf.apache.org/core" 
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://cxf.apache.org/core 
    http://cxf.apache.org/schemas/core.xsd
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://cxf.apache.org/jaxrs 
    http://cxf.apache.org/schemas/jaxrs.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <jaxrs:server id="base" address="/rest">
             <jaxrs:features>
               <cxf:logging/>
           </jaxrs:features>
           <jaxrs:serviceBeans>
                <ref bean="MultiArgs" />
           </jaxrs:serviceBeans>
           </jaxrs:server>
             <bean id="MultiArgs" class="com.multiArgs.MultiArgsImpl" />
    </beans>

我收到了

的记录消息
18:48:55,168  INFO ContextLoader:194 - Root WebApplicationContext: initialization started
18:48:55,200  INFO XmlWebApplicationContext:456 - Refreshing Root WebApplicationContext: startup date [Tue Nov 17 18:48:55 IST 2015]; root of context hierarchy
18:48:55,231  INFO XmlBeanDefinitionReader:315 - Loading XML bean definitions from ServletContext resource [/WEB-INF/cxf.xml]
18:48:55,293  INFO XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [META-INF/cxf/cxf.xml]
18:48:55,418  INFO DefaultListableBeanFactory:557 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@8245e9: defining beans [cxf,org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor,org.apache.cxf.bus.spring.Jsr250BeanPostProcessor,org.apache.cxf.bus.spring.BusExtensionPostProcessor,base,MultiArgs]; root of factory hierarchy
18:48:56,883  INFO ContextLoader:221 - Root WebApplicationContext: initialization completed in 1699 ms

上面只有我明白了。但我也想要入站和出站邮件。任何人帮助我......

2 个答案:

答案 0 :(得分:1)

您已启用的日志记录功能将使用该课程:org.apache.cxf.feature.LoggingFeature

要启用有效负载的打印,作为入站/出站的一部分,请将prettyLogging属性设置为 true

以下是代码配置:

<bean id="loggingFeature" class="org.apache.cxf.feature.LoggingFeature">
    <property name="prettyLogging" value="true" />
</bean>

然后参考bean:

 <jaxrs:features>
     <ref bean="loggingFeature"/>
</jaxrs:features>

这应记录所有RESTFul请求/响应

答案 1 :(得分:0)

您需要更改cxf.xml文件,如下所示。

<form class="form-horizontal" action="">
    <div class="form-group">
        <input type="text" name="key_word" class="form-control" value="">
        <input type="text" name="test" class="form-control" value="">
        <button type="submit" class="btn btn-primary btn-bg-red">
            GO
        </button>
    </div>
</form>