Java 如何配置日志记录来记录CXF入站和出站restful消息?
我使用log4j而不是java.util.logging创建了restfulweb服务。但它没有将入站和出站的消息写入日志文件 下面是消息日志记录配置。 我创建了META-INF/cxf/org.apache.cxf.Logger文件。然后我把下面的配置行Java 如何配置日志记录来记录CXF入站和出站restful消息?,java,spring,apache,web-services,logging,Java,Spring,Apache,Web Services,Logging,我使用log4j而不是java.util.logging创建了restfulweb服务。但它没有将入站和出站的消息写入日志文件 下面是消息日志记录配置。 我创建了META-INF/cxf/org.apache.cxf.Logger文件。然后我把下面的配置行 org.apache.cxf.common.logging.Log4jLogger 然后我创建了WEB-INF/classes/log4j.properties配置文件。以下为log4j配置 # Root logger option log
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>
以上只有我知道了。但我也需要入站和出站消息。任何人都可以帮助我…您需要更改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" />
<cxf:bus>
<cxf:features>
<cxf:logging/>
</cxf:features>
</cxf:bus>
<jaxrs:server id="base" address="/rest">
<jaxrs:serviceBeans>
<ref bean="MultiArgs" />
</jaxrs:serviceBeans>
</jaxrs:server>
<bean id="MultiArgs" class="com.multiArgs.MultiArgsImpl" />
</beans>
您启用的日志功能将使用类:
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请求/响应启用总线上的日志记录,将记录包括的所有调用SOAP/jax ws调用。您的问题得到答案了吗
<jaxrs:features>
<ref bean="loggingFeature"/>
</jaxrs:features>