2011-12-29 102 views
2

我目前使用log4j来记录CXF,如CXF用户指南中所述。但是日志文件泛滥,并且对于所有IN/OUT负载日志变得难以管理。CXF日志记录拦截器

只有当生成一些故障/异常作为输出时,我需要记录传入的SOAP有效负载。我知道这将需要编写自定义拦截器,但这是可能实现的吗?

任何人都可以提供一些链接/提示或可能是一些示例工作代码?

由于提前 教Tirthankar

+0

有人请建议......我仍然卡住!祝大家新年快乐。 – Tirtha 2012-01-01 05:30:59

回答

3

此链接可以帮助:http://www.madbit.org/blog/programming/942/how-to-log-apache-cxf-soap-request-and-response-using-log4j/#sthash.SOlB7sx6.CaTMsv3I.dpbs

你可以做到这一点通过编写自定义拦截器,并添加豆类裁判在你cxf.xml文件如下:

<bean id="customIncomingSoapFaultInterceptor"  class="com.tirtha.CustomIncomingSoapFaultInterceptor" /> 

<cxf:bus> 

    <cxf:inFaultInterceptors> 
     <ref bean="customIncomingSoapFaultInterceptor" /> 

    </cxf:inFaultInterceptors> 

</cxf:bus> 

自定义拦截器示例:

import java.io.IOException; 
import java.io.InputStream; 
import javax.servlet.http.HttpServletRequest; 
import javax.xml.namespace.QName; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 
import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import org.apache.cxf.binding.soap.Soap12; 
import org.apache.cxf.binding.soap.SoapMessage; 
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor; 
import org.apache.cxf.helpers.IOUtils; 
import org.apache.cxf.interceptor.Fault; 
import org.apache.cxf.io.CachedOutputStream; 
import org.apache.cxf.phase.Phase; 
import org.apache.cxf.transport.http.AbstractHTTPDestination; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 

public class CustomIncomingSoapFaultInterceptor extends AbstractSoapInterceptor { 

private static Log s_logger = LogFactory.getLog(CustomIncomingSoapFaultInterceptor.class); 


public CustomIncomingSoapFaultInterceptor(){ 

    // set phase here 
    //super(Phase.PRE_PROTOCOL); 
    super(Phase.RECEIVE); 
} 
@Override 
public void handleMessage(SoapMessage message) throws Fault { 
    Fault fault = null; 
    String soapMessage = null; 

     StringBuilder strMessage = null;  
     HttpServletRequest httpRequest = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST); 
     if (httpRequest != null) { 


      InputStream ist = message.getContent(InputStream.class); 
       if (ist != null) { 
        CachedOutputStream bos = new CachedOutputStream(); 
        try { 
         IOUtils.copy(ist, bos); 

         bos.flush(); 
         ist.close(); 
         message.setContent(InputStream.class, bos.getInputStream()); 
         soapMessage = new String(bos.getBytes());//this soap message is what you want 
         bos.close(); 

         s_logger.debug("Soap Message: ---------->" + soapMessage==null?"null":soapMessage); 
         s_logger.debug("String Request: ---------->" + soapMessage); 


        } catch (IOException e) { 
         throw new Fault(e); 
        } 
       } 



     } 
} 
0

您需要自定义Feature才能记录SOAP故障/异常。

以下是LoggingFeature.initializeProvider()方法的源代码。正如你所看到的,故障拦截器正在被添加到这个方法中。

@Override 
protected void initializeProvider(InterceptorProvider provider, Bus bus) { 
    if (limit == DEFAULT_LIMIT && inLocation == null 
     && outLocation == null && !prettyLogging) { 
     provider.getInInterceptors().add(IN); 
    >>> provider.getInFaultInterceptors().add(IN); 
     provider.getOutInterceptors().add(OUT); 
    >>> provider.getOutFaultInterceptors().add(OUT); 
    } else { 
     LoggingInInterceptor in = new LoggingInInterceptor(limit); 
     in.setOutputLocation(inLocation); 
     in.setPrettyLogging(prettyLogging); 
     in.setShowBinaryContent(showBinary); 
     LoggingOutInterceptor out = new LoggingOutInterceptor(limit); 
     out.setOutputLocation(outLocation); 
     out.setPrettyLogging(prettyLogging); 
     out.setShowBinaryContent(showBinary); 

     provider.getInInterceptors().add(in); 
     provider.getInFaultInterceptors().add(in); 
     provider.getOutInterceptors().add(out); 
     provider.getOutFaultInterceptors().add(out); 
    } 
} 

您可以编写自己的LoggingFeature并覆盖initializeProvider如下:

public class CustomLoggingFeature extends LoggingFeature { 
    @Override 
    protected void initializeProvider(InterceptorProvider provider, Bus bus) { 
     provider.getInFaultInterceptors().add(new LoggingInInterceptor(getLimit())); 
     provider.getOutFaultInterceptors().add(new LoggingOutInterceptor(getLimit())); 
    } 
} 

然后,你可以激活CustomLoggingFeature如下:

@WebService 
@Features(classes = {CustomLoggingFeature.class}) 
public interface AssetServices { 
}