How to log Apache CXF Soap Request and Soap Response using Log4j?

前端 未结 10 1020
谎友^
谎友^ 2020-11-29 19:07

I am using the Apache CXF Framework. Inside my client program, I need to log CXF SOAP Requests and SOAP Responses. When I used



        
相关标签:
10条回答
  • 2020-11-29 19:47

    You need to create a file named org.apache.cxf.Logger (that is: org.apache.cxf file with Logger extension) under /META-INF/cxf/ with the following contents:

    org.apache.cxf.common.logging.Log4jLogger
    

    Reference: Using Log4j Instead of java.util.logging.

    Also if you replace standard:

    <cxf:bus>
      <cxf:features>
        <cxf:logging/>
      </cxf:features>
    </cxf:bus>
    

    with much more verbose:

    <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>
    

    Apache CXF will pretty print XML messages formatting them with proper indentation and line breaks. Very useful. More about it here.

    0 讨论(0)
  • 2020-11-29 19:52

    Another easy way is to set the logger like this- ensure that you do it before you load the cxf web service related classes. You can use it in some static blocks.

    YourClientConstructor() {
    
      LogUtils.setLoggerClass(org.apache.cxf.common.logging.Log4jLogger.class);
    
      URL wsdlURL = YOurURL;//
    
      //create the service
      YourService = new YourService(wsdlURL, SERVICE_NAME);
      port = yourService.getServicePort(); 
    
      Client client = ClientProxy.getClient(port);
      client.getInInterceptors().add(new LoggingInInterceptor());
      client.getOutInterceptors().add(new LoggingOutInterceptor());
    }
    

    Then the inbound and outbound messages will be printed to Log4j file instead of the console. Make sure your log4j is configured properly

    0 讨论(0)
  • 2020-11-29 19:52

    cxf.xml

    <cxf:bus>
        <cxf:ininterceptors>
            <ref bean="loggingInInterceptor" />
        </cxf:ininterceptors>
        <cxf:outinterceptors>
            <ref bean="logOutInterceptor" />
        </cxf:outinterceptors>
    </cxf:bus>
    

    org.apache.cxf.Logger

    org.apache.cxf.common.logging.Log4jLogger
    

    Please check screenshot here

    0 讨论(0)
  • 2020-11-29 19:57

    This worked for me.

    Setup log4j as normal. Then use this code:

        // LOGGING 
        LoggingOutInterceptor loi = new LoggingOutInterceptor(); 
        loi.setPrettyLogging(true); 
        LoggingInInterceptor lii = new LoggingInInterceptor(); 
        lii.setPrettyLogging(true); 
    
        org.apache.cxf.endpoint.Client client = org.apache.cxf.frontend.ClientProxy.getClient(isalesService); 
        org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint(); 
    
        cxfEndpoint.getOutInterceptors().add(loi); 
        cxfEndpoint.getInInterceptors().add(lii);
    
    0 讨论(0)
  • 2020-11-29 19:58

    In case somebody wants to do this, using Play Framework (and using LogBack http://logback.qos.ch/), then you can configure the application-logger.xml with this line:

     <logger name="org.apache.cxf" level="DEBUG"/>
    

    For me, it did the trick ;)

    0 讨论(0)
  • 2020-11-29 20:01

    In your spring context configuring below would log the request and response soap messsage.

    <bean id="loggingFeature" class="org.apache.cxf.feature.LoggingFeature">
        <property name="prettyLogging" value="true" />
    </bean>
    
    <cxf:bus>
        <cxf:features>
            <ref bean="loggingFeature" />
        </cxf:features>
    </cxf:bus>
    
    0 讨论(0)
提交回复
热议问题