cxf inbound and outbound message logging to the separate log file

时光总嘲笑我的痴心妄想 提交于 2020-01-22 23:00:28

问题


I looked up all messages but did not find a clear answer for that question.

How can I configure logging to log CXF inbound and outbound restful messages ?

I have the following setup.

  • File org.apache.cxf.Logger with

    org.apache.cxf.common.logging.Log4jLogger
    
  • 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>
    

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.

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/

Thanks.

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>

回答1:


Assuming you are using CXF 2.2.8 or higher, you would need to do the following:

step 1) Create a file META-INF/cxf/org.apache.cxf.Logger on the classpath containing the following:

org.apache.cxf.common.logging.Slf4jLogger

If you're using Maven, this new file must be in src/main/resources/META-INF/cxf, not below src/main/webapp!

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.

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>

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>

I've created a blog post which explains how to configure CXF for log4j in more detail.



来源:https://stackoverflow.com/questions/9424058/cxf-inbound-and-outbound-message-logging-to-the-separate-log-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!