log4j CustomOutInterceptor始终获取超类的示例

bq3bfh9z  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(171)

为了记录SOAP请求和响应负载,我已经启用了cxf日志记录,并使用了默认的拦截器,它可以无缝地工作。但是客户端还希望在LoggingMessage信息中添加Transaction-ID。为了实现这一点,我创建了两个新的自定义拦截器,它们扩展了默认的LoggingInInterceptorLoggingOutInterceptor
对于inBounds,我覆盖了formatLoggingMessage方法,并在登录控制台之前更改了消息。这很好用,但对于outBounds负载,它调用了超类(LoggingOutInterceptor)方法,而不是调用覆盖的方法。我已经多次检查配置,但无法解决此问题。
下面是代码片段。

适配器CXF日志侦听器输出.java

public class AdaptorCXFLoggingInterceptorOut extends LoggingOutInterceptor
{

    @Override
    protected String formatLoggingMessage(LoggingMessage loggingMessage)
    {
        StringBuilder transactionId = new StringBuilder("\nTransaction-ID : ");
        transactionId.append(MDC.get("TRANSACTION_ID"));
        StringBuilder loggingMsg = new StringBuilder(loggingMessage.toString());
        int indexOfID = loggingMsg.indexOf("Address:");
        loggingMsg.insert(indexOfID-1, transactionId);
        return loggingMsg.toString();
    }

cxf.xml文件

<bean id="loggingFeature" class="org.apache.cxf.feature.LoggingFeature">
        <property name="prettyLogging" value="true" />
    </bean>
    <bean id="logInbound" class="com.seamless.ers.interceptors.AdaptorCXFLoggingInterceptorIn"/>
    <bean id="logOutbound" class="com.seamless.ers.interceptors.AdaptorCXFLoggingInterceptorOut"/>
    <cxf:bus>
        <cxf:inInterceptors>
            <ref bean="logInbound"/>
        </cxf:inInterceptors>
        <cxf:outInterceptors>
            <ref bean="logOutbound"/>
        </cxf:outInterceptors>
        <cxf:features>
            <ref bean="loggingFeature" />
        </cxf:features>
    </cxf:bus>

日志

--------------------------------------
2022-09-27T14:54:56,033 INFO  [ERSWSReseller] -  Inbound Message
---------------------------
ID: 5 
<Transaction-ID> <--------- MISSING HERE
Address: http://localhost:8777/txe/reseller
Encoding: UTF-8
Http-Method: POST
----------------------------
ID: 5
Transaction-ID : 16a21796-0d96-4b9f-9951-fe6a877097fd <---- works for InBound
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=utf-8

在下面的图像中,我们可以看到它引用的是超级调用的示例,而不是customOutInterceptor。

有谁能告诉我,我错过了什么。提前感谢。

s2j5cfk0

s2j5cfk01#

在启用cxf日志到DEBUG级别后,我开始知道拦截器没有添加到链中,它被bus跳过了,因为我没有将phase方法添加到该拦截器。与此沿着,我们还需要确保我们的拦截器必须在LoggingOutInterceptor之前获得链。
通过提及相位,它完美地工作。

日志

2022-09-27T18:43:34,993 DEBUG [ClientImpl] -  Interceptors contributed by bus: [com.seamless.ers.interceptors.AdaptorCXFLoggingInterceptorOut@68205edc, org.apache.cxf.ws.policy.PolicyOutInterceptor@2ea3817d, org.apache.cxf.interceptor.LoggingOutInterceptor@21977a79]

Adding interceptor com.seamless.ers.interceptors.AdaptorCXFLoggingInterceptorOut@68205edc to phase pre-stream

  setup [PolicyOutInterceptor]
  pre-logical [HolderOutInterceptor, SwAOutInterceptor, WrapperClassOutInterceptor, SoapHeaderOutFilterInterceptor]
  post-logical [SoapPreProtocolOutInterceptor]
  prepare-send [MessageSenderInterceptor]
  pre-stream [AdaptorCXFLoggingInterceptorOut, LoggingOutInterceptor, AttachmentOutInterceptor, StaxOutInterceptor]
  write [SoapOutInterceptor]
  marshal [WrappedOutInterceptor, BareOutInterceptor]
  send [WebServiceAddressLoggingInterceptor]

cxf.xml文件

<bean id="logOutbound" class="com.seamless.ers.interceptors.AdaptorCXFLoggingInterceptorOut">
        <constructor-arg value="pre-stream"/>
    </bean>

"拦截者"

public class AdaptorCXFLoggingInterceptorOut extends LoggingOutInterceptor
{
    public AdaptorCXFLoggingInterceptorOut(String phase)
    {
        super(phase);
        addBefore(LoggingOutInterceptor.class.getName());
    }

    @Override
    protected String formatLoggingMessage(LoggingMessage loggingMessage)
    {
        StringBuilder transactionId = new StringBuilder("\nUnique-Identifier-ID : ");
        transactionId.append(MDC.get("TRANSACTION_ID"));
        StringBuilder loggingMsg = new StringBuilder(loggingMessage.toString());
        int indexOfID = loggingMsg.indexOf("Address:");
        loggingMsg.insert(indexOfID-1, transactionId);
        return loggingMsg.toString();
    }
}

相关问题