17

I am using Apache CXF with Spring Boot with the help of cxf-spring-boot-starter-jaxws plugin of version 3.2.7.

My intention is to customize the LoggingInterceptors but when I created the below class:

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

but my IDE strikes out the LoggingInInterceptor complaining it's deprecated with the explanation

use logging module rt/features/logging instead

So how should one go about customizing the logging interceptor using this module ?

alegria
  • 931
  • 2
  • 10
  • 25

4 Answers4

31

What this message is telling you, is to use the Apache CXF Advanced logging feature module.

Its dependency is (latest version)

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

Inside you'll find a comparable org.apache.cxf.ext.logging.LoggingInInterceptor (link)


I'm not a CXF user, however I suppose you'll have to interact with a JaxWsProxyFactoryBean.
Remember you need to use the same version for all the CXF modules.

After getting an hold on it, you can do

factory.getInInterceptors().add(new MyCustomInterceptor());
LppEdd
  • 20,274
  • 11
  • 84
  • 139
  • Thank you for the answer. Though, the API has changed it seems. Gonna have to figure out how to replace the removed methods such as `formatLoggingMessage()`. Please feel free to share further info on migrating to this new module. Cheers – alegria Mar 05 '19 at 04:50
  • @dashboard look for `PrintWriterEventSender` and at the source code if `LoggingInInterceptor` https://github.com/apache/cxf/blob/master/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingInInterceptor.java – LppEdd Mar 05 '19 at 08:19
  • @dashboard look also at second constructor and the last line of `LoggingInInterceptor#handleMessage` – LppEdd Mar 05 '19 at 08:20
  • For anyone wondering how to use this from a Spring XML config, here is an example: https://cxf.apache.org/docs/message-logging.html#MessageLogging-ManualUsage Basically, one needs to define `` inside the `jaxws:features` section. – vadipp Apr 21 '19 at 04:19
  • thx. found your answer after a whole day of searching. And it's needed to add just one dependency... – Filomat Nov 15 '19 at 20:38
  • @Filomat I know that feeling :P Glad this was helpful. – LppEdd Nov 15 '19 at 20:48
15

Basically 4 things are needed to update from the old to the new cxf logging (rt/features/logging).

First, Set the logging feature:

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

You don't need anymore the interceptors (in case you used them, delete them):

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

Second, create your LoggingFeature:

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

Third, create your EventLogSender:

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

Fourth, create a CustomMasker class where you have your own string manipulation logic to mask the desired information.

Let me know if it worked!

RichArt
  • 1,534
  • 18
  • 35
  • Thank you for the input, will try as soon as got time. If this works you'd be answered the second part of the question, however I can only choose one answer. Maybe you can update the answer so it has two sections one for existing and one for your answer ? – alegria Aug 07 '19 at 07:12
  • Well the thing is that with the new cxf logging you don't customize the `LoggingInInterceptor` class, instead you do the things I described above. Or maybe I didn't understand your comment? What information exactly is missing? – RichArt Aug 09 '19 at 15:55
  • There's no information missing, i thought that it'd be better if you were to be so kind to update the accepted answer so it contains your part too, that's all :) – alegria Aug 13 '19 at 16:34
4

When you have this elsewhere mentioned dependency:

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

and when you work with a JaxWsProxyFactoryBean, you can configure that factory e.g. like this:

LoggingFeature loggingFeature = new LoggingFeature();
loggingFeature.setPrettyLogging(true);
loggingFeature.setVerbose(true);
loggingFeature.setLogMultipart(true);
factory.getFeatures().add(loggingFeature);
Borze
  • 41
  • 2
0

The new imports for this class are :

import org.apache.cxf.ext.logging.LoggingInInterceptor;
import org.apache.cxf.ext.logging.LoggingOutInterceptor;

which could be found in the org.apache.cxf:cxf-rt-features-logging dependency.

Please refer to this question for code snippets.

Pierre C
  • 2,920
  • 1
  • 35
  • 35