cxf入站和出站消息记录到单独的日志文件 [英] cxf inbound and outbound message logging to the separate log file
问题描述
我查看了所有消息,但没有找到该问题的明确答案.
I looked up all messages but did not find a clear answer for that question.
如何配置日志记录以记录CXF入站和出站的静态消息?
How can I configure logging to log CXF inbound and outbound restful messages ?
我有以下设置.
-
使用
File org.apache.cxf.Logger with
org.apache.cxf.common.logging.Log4jLogger
applicationContext.xml具有以下内容(听起来很傻,但这是我可以获取消息输出的拦截器的唯一位置)
applicationContext.xml has the following (it sounds silly, but it is the only place for interceptors I could get messages output)
<bean id="abstractLoggingInterceptor" abstract="true">
<property name="prettyLogging" value="true"/>
</bean>
<bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"
parent="abstractLoggingInterceptor"/>
<bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"
parent="abstractLoggingInterceptor"/>
<cxf:bus>
<cxf:inInterceptors>
<ref bean="loggingInInterceptor"/>
</cxf:inInterceptors>
<cxf:outInterceptors>
<ref bean="loggingOutInterceptor"/>
</cxf:outInterceptors>
<cxf:outFaultInterceptors>
<ref bean="loggingOutInterceptor"/>
</cxf:outFaultInterceptors>
<cxf:inFaultInterceptors>
<ref bean="loggingInInterceptor"/>
</cxf:inFaultInterceptors>
</cxf:bus>
我尝试按照slf4j和log4j的说明进行操作,但是我获得的唯一输出是应用程序日志消息.我可以在控制台上看到入站和出站消息.
I tried to follow these instructions with slf4j and with log4j, but the the only output I get to the file is application log messages. I can see inbound and outbound messages on my console.
我可以得到类似于logback.xml的信息吗,所以我将应用程序日志和消息日志分开.例如: http://www.wolfe.id.au/2011/05/20/apache-cxf-logging/
Can I get something similar to logback.xml work for me, so I separate app logs and message logs. Example: http://www.wolfe.id.au/2011/05/20/apache-cxf-logging/
谢谢.
我从类路径中删除了org.apache.cxf.common.logging.Log4jLogger,并将以下内容放入了log4j.xml中.当日志记录级别等于INFO时,它将记录到文件并进行控制台.
EDIT 1: I removed org.apache.cxf.common.logging.Log4jLogger from my classpath, and placed the following to my log4j.xml. It logging to the file and to console when the level of logging is equal to INFO.
<appender name="RSLOGFILE" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${project.basedir}/logs/cxf_inout_messages.log"/>
<param name="MaxFileSize" value="100KB"/>
<!-- Keep one backup file -->
<param name="MaxBackupIndex" value="1"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- Print the date in ISO 8601 format -->
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/>
</layout>
</appender>
<logger name="org.apache.cxf">
<level value="ERROR"/>
<appender-ref ref="RSLOGFILE"/>
</logger>
推荐答案
假设您使用的是CXF 2.2.8或更高版本,则需要执行以下操作:
Assuming you are using CXF 2.2.8 or higher, you would need to do the following:
步骤1)在包含以下内容的类路径上创建文件META-INF/cxf/org.apache.cxf.Logger
:
step 1) Create a file META-INF/cxf/org.apache.cxf.Logger
on the classpath containing the following:
org.apache.cxf.common.logging.Slf4jLogger
如果您使用的是Maven,则此新文件必须位于src/main/resources/META-INF/cxf
中,且不能低于src/main/webapp
!
If you're using Maven, this new file must be in src/main/resources/META-INF/cxf
, not below src/main/webapp
!
步骤2)如果要记录所有消息,请创建CXF LoggingFeature
,将prettyLogging
属性设置为true
,然后将其添加到CXF总线.
step 2) If you want to log all messages, create a CXF LoggingFeature
, set the prettyLogging
property to true
and add it to the CXF bus.
步骤3)为log4j和slf4j-log4j12添加所需的jar文件.如果您使用的是Maven,请包括以下依赖项:
step 3) Add the needed jar files for log4j and slf4j-log4j12. If you are using Maven, include following dependencies:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
步骤4):在log4j.xml
中,将org.apache.cxf.services
的日志级别设置为INFO,将additivity
设置为FALSE
并附加专用的附加程序:
step 4) In you log4j.xml
set the log level of org.apache.cxf.services
to INFO, set additivity
to FALSE
and attach a dedicated appender:
<!-- level INFO needed to log SOAP messages -->
<logger name="org.apache.cxf.services" additivity="false">
<level value="INFO" />
<!-- specify a dedicated appender for the SOAP messages -->
<appender-ref ref="WS_LOG_FILE" />
</logger>
我创建了 查看全文