如何使用Log4j记录Apache CXF Soap请求和Soap响应?

Paw*_*wan 40 log4j cxf

我正在使用Apache CXF框架.在我的客户端程序中,我需要记录CXF SOAP请求和SOAP响应.我用的时候

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress(host);
factory.setServiceClass(MyService.class);
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
Run Code Online (Sandbox Code Playgroud)

我在控制台中获得了这些SOAP请求和SOAP响应:

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
--------------------------------------
Run Code Online (Sandbox Code Playgroud)

但我的实际要求是,我需要将它们放在日志文件中,而不是将它们打印到服务器控制台.

当我直接使用log4j时如图所示

log4j(factory.getInInterceptors().add(new LoggingInInterceptor()));
log4j(factory.getOutInterceptors().add(new LoggingOutInterceptor()));
Run Code Online (Sandbox Code Playgroud)

它只打印truetrue在日志文件中.

有人可以告诉我如何配置吗?

Tom*_*icz 68

您需要使用以下内容创建一个名为org.apache.cxf.Logger(即:org.apache.cxfLogger扩展名的文件)的文件/META-INF/cxf/:

org.apache.cxf.common.logging.Log4jLogger
Run Code Online (Sandbox Code Playgroud)

参考:使用Log4j而不是java.util.logging.

如果你更换标准:

<cxf:bus>
  <cxf:features>
    <cxf:logging/>
  </cxf:features>
</cxf:bus>
Run Code Online (Sandbox Code Playgroud)

更冗长:

<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>
Run Code Online (Sandbox Code Playgroud)

Apache CXF将使用适当的缩进和换行符来格式化XML消息.很有用.更多关于它的信息.

  • 确保`org.apache.cxf`记录器设置为`ALL`或`DEBUG`. (4认同)
  • **file** 必须命名为 `org.apache.cxf` 并带有 `Logger` 扩展名。据我所知,您已经创建了一个文件 `\META-INF\cxf\org\apache\cxf\Logger.class` 而不是 `\META-INF\cxf\org.apache.cxf.Logger`。我从来没有说过你需要创建一个`\org\apache\cxf`的目录结构。这是文件名(尴尬,我必须承认)。 (2认同)
  • 好的,看看[使用Log4j而不是java.util.logging](http://cxf.apache.org/docs/debugging-and-logging.html#DebuggingandLogging-UsingLog4jInsteadofjava.util.logging),这真的是在项目中创建单行文件的问题...... (2认同)

Vin*_*ins 13

另一种简单的方法是像这样设置记录器 - 确保在加载与cxf Web服务相关的类之前执行此操作.您可以在一些静态块中使用它.

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());
}
Run Code Online (Sandbox Code Playgroud)

然后,入站和出站消息将打印到Log4j文件而不是控制台.确保正确配置了log4j


小智 11

在Preethi Jain szenario中实现漂亮记录的最简单方法:

LoggingInInterceptor loggingInInterceptor = new LoggingInInterceptor();
loggingInInterceptor.setPrettyLogging(true);
LoggingOutInterceptor loggingOutInterceptor = new LoggingOutInterceptor();
loggingOutInterceptor.setPrettyLogging(true);
factory.getInInterceptors().add(loggingInInterceptor);
factory.getOutInterceptors().add(loggingOutInterceptor);
Run Code Online (Sandbox Code Playgroud)

  • 不推荐使用LoggingInInceptor和LoggingOutInterceptor:"使用日志记录模块rt/features/logging" (2认同)

Man*_*ath 5

在您的 spring 上下文中,下面的配置将记录请求和响应肥皂消息。

<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>
Run Code Online (Sandbox Code Playgroud)


Al *_*ant 5

这对我有用。

正常设置 log4j。然后使用这段代码:

    // 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);
Run Code Online (Sandbox Code Playgroud)