[英]How can I configure logging to log CXF inbound and outbound restful messages?
我在使用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
以上只有我明白了。 但我也想要入站和出站邮件。 有人帮我...
您启用的日志记录功能将使用以下类: 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请求/响应
您需要如下更改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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.