【问题标题】:Apache CXF LoggingInInterceptor is deprecated - what to use instead?Apache CXF LoggingInInterceptor 已弃用 - 改用什么?
【发布时间】:2019-07-26 03:50:46
【问题描述】:

我在 3.2.7 版的 cxf-spring-boot-starter-jaxws 插件的帮助下将 Apache CXF 与 Spring Boot 一起使用。

我的意图是自定义 LoggingInterceptors,但是当我创建以下类时:

public class CustomLoggingInInterceptor extends org.apache.cxf.interceptor.LoggingInInterceptor {}

但是我的 IDE 删除了 LoggingInInterceptor 抱怨它已被弃用的解释

改用日志模块 rt/features/logging

那么应该如何使用这个模块自定义日志拦截器呢?

【问题讨论】:

  • 我在答案中添加了更多细节。我认为它们应该足够了。

标签: java logging cxf interceptor


【解决方案1】:

这条消息告诉你的是使用Apache CXF Advanced logging feature 模块。

它的依赖是(最新版本)

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-features-logging</artifactId>
    <version>3.3.0</version>
    <scope>test</scope>
</dependency>

在里面你会发现一个类似的org.apache.cxf.ext.logging.LoggingInInterceptor (link)


我不是 CXF 用户,但我想您必须与 JaxWsProxyFactoryBean 进行交互。
请记住,您需要对所有 CXF 模块使用相同的版本。

掌握了之后就可以了

factory.getInInterceptors().add(new MyCustomInterceptor());

【讨论】:

  • 感谢您的回答。不过,API 似乎已经改变了。必须弄清楚如何替换已删除的方法,例如formatLoggingMessage()。请随时分享有关迁移到此新模块的更多信息。干杯
  • @dashboard 查找PrintWriterEventSender 并在源代码中查找LoggingInInterceptor github.com/apache/cxf/blob/master/rt/features/logging/src/main/…
  • @dashboard 查看第二个构造函数和LoggingInInterceptor#handleMessage的最后一行
  • 对于任何想知道如何从 Spring XML 配置中使用它的人,这里有一个示例:cxf.apache.org/docs/… 基本上,需要在 jaxws:features 部分中定义 &lt;bean class="org.apache.cxf.ext.logging.LoggingFeature"/&gt;
  • 谢谢。经过一整天的搜索,找到了您的答案。并且只需要添加一个依赖项...
【解决方案2】:

从旧的 cxf 日志更新到新的 cxf 日志记录(rt/features/logging)基本上需要 4 件事。

首先,设置日志功能:

final JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setFeatures(Collections.singletonList(new CustomLoggingFeature()));

您不再需要拦截器(如果您使用了它们,请删除它们):

factory.getInInterceptors().add(new CustomMaskedLoggingInInterceptor()); factory.getOutInterceptors().add(new CustomMaskedLoggingOutInterceptor());

其次,创建您的 LoggingFeature:

public class CustomLoggingFeature extends org.apache.cxf.ext.logging.LoggingFeature {
    public CustomLoggingFeature() {
        super();
        this.setSender(new CustomEventLogSender());
    }
}

第三,创建你的 EventLogSender:

public class CustomEventLogSender extends Slf4jVerboseEventSender {
    @Override
    protected String getLogMessage(LogEvent event) {
        String logMessage = super.getLogMessage(event);
        return CustomMasker.mask(logMessage);
    }
}

第四,创建一个 CustomMasker 类,您可以在其中拥有自己的字符串操作逻辑来屏蔽所需的信息。

让我知道它是否有效!

【讨论】:

  • 感谢您的意见,有时间会尽快尝试。如果这可行,您将得到问题的第二部分的回答,但是我只能选择一个答案。也许你可以更新答案,让它有两个部分,一个是现有的,一个是你的答案?
  • 那么问题是,使用新的 cxf 日志记录,您无需自定义 LoggingInInterceptor 类,而是执行我上面描述的操作。或者也许我不明白你的评论?究竟缺少什么信息?
  • 没有任何信息丢失,我认为如果您能善意地更新已接受的答案以便它也包含您的部分会更好,仅此而已:)
【解决方案3】:

当你有这个其他地方提到的依赖时:

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-features-logging</artifactId>
    <version>${org.apache.cxf.version}</version>
</dependency>

当您使用JaxWsProxyFactoryBean 时,您可以配置该工厂,例如像这样:

LoggingFeature loggingFeature = new LoggingFeature();
loggingFeature.setPrettyLogging(true);
loggingFeature.setVerbose(true);
loggingFeature.setLogMultipart(true);
factory.getFeatures().add(loggingFeature);

【讨论】:

    猜你喜欢
    • 2017-11-22
    • 2016-02-17
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    • 1970-01-01
    相关资源
    最近更新 更多