Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

log4j

cxf

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

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setAddress(host); factory.setServiceClass(MyService.class); factory.getInInterceptors().add(new LoggingInInterceptor()); factory.getOutInterceptors().add(new LoggingOutInterceptor()); 

I got these SOAP Request and SOAP Responses in the console:

Nov 9, 2011 6:48:01 PM org.apache.cxf.interceptor.LoggingOutInterceptor$LoggingCallback onClose INFO: Outbound Message --------------------------- ID: 2 Encoding: UTF-8 Content-Type: text/xml Headers: {} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns4:MYResponse -------------------------------------- 

But my actual requirement is, instead of printing them to the server console, I need to have them inside the log file.

When I used log4j directly as shown

log4j(factory.getInInterceptors().add(new LoggingInInterceptor())); log4j(factory.getOutInterceptors().add(new LoggingOutInterceptor())); 

It is only printing true and true inside the log file.

Could anybody please let me know how to configure this?

like image 260
Pawan Avatar asked Nov 09 '11 13:11

Pawan


People also ask

How do you trace a SOAP request and response?

In SOAP web service, each HTTP request or response encapsulates a SOAP envelope, these messages are easy to trace by using Eclipse IDE, build-in “TCP/IP monitor” tool. The idea is host another server in between the client and server to perform port forward function to intercept the HTTP traffic.

How do I find SOAP requests?

SOAP messages are transported by HTTP protocol. To view the HTTP request, click RAW at SoapUI Request window (left side). The Request is posted to the web-server. Hence, the POST method of Http is used.


2 Answers

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

like image 30
Vins Avatar answered Sep 27 '22 23:09

Vins


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.

like image 153
Tomasz Nurkiewicz Avatar answered Sep 27 '22 21:09

Tomasz Nurkiewicz