venkat
venkat

Reputation: 39

how to log SOAP request/response using log4j2 in separate log files dynamically

Need to write separate log files based on an apikey using log4j2 for SOAP request and response. we are client side or consuming SOAP webservice.

I am able to write separate log files based on an apikey for REST request & response but not sure how to do for SOAP request & response

code to write logs in separate log files is as follows

Need to write separate log files based on an apikey using log4j2 for SOAP request and response. we are client side or consuming SOAP webservice.

I am able to write separate log files based on an apikey for REST request & response but not sure how to do for SOAP request & response

code to write logs in separate log files is as follows :

public final class SPRestLog4j2Logger extends org.apache.logging.log4j.core.async.AsyncLogger {

    private SPRestLog4j2Logger(LoggerContext context, String name, MessageFactory msgFactory) {
        super(context, name, msgFactory);
        this.setLevel(Level.ALL);
    }

    public static SPRestLog4j2Logger configureCategoryLogger(String apiKey, String loggingPath) {
        LoggerContext context = new AsyncLoggerContext(apiKey);
        MessageFactory msgFactory = new FormattedMessageFactory();
        SPRestLog4j2Logger logger = new SPRestLog4j2Logger(context, apiKey, msgFactory);

        RandomAccessFileAppender appender = RandomAccessFileAppender
            .createAppender(
                loggingPath + apiKey + ".log", // filename
                "true", // append
                "file_appender-" + apiKey, // name
                "true", // immediateFlush
                "", // bufferSize
                "true", // ignoreExceptions
                PatternLayout.createLayout(
                    "%-5p - [%d] - [%t] - [%l]  : %m%n", null,
                    null, Charset.forName("UTF-8"), true, true, apiKey, apiKey), null, // filter
                "false", // advertise
                null, // advertiseURI
                null // config
            );

        ConsoleAppender consoleAppender = ConsoleAppender.createAppender(
            PatternLayout.createLayout(
                "%-5p - [%d] - [%t] - [%l]  : %m%n", null,
                null, Charset.forName("UTF-8"), true, true, apiKey, apiKey), null, null,
            "Console", null, null);

        appender.start();
        consoleAppender.stop();
        logger.getContext().getConfiguration().getLoggerConfig(apiKey)
            .addAppender(appender, Level.TRACE, null);
        logger.getContext().getConfiguration().getLoggerConfig(apiKey)
            .addAppender(consoleAppender, Level.OFF, null);
        return logger;
    }
}

code used to write SOAP request / response to file is as follows :

SalesCustomerSelectService customerService = new SalesCustomerSelectService(wsdlURL, SERVICE_NAME);
        SalesCustomerSelect customerPort = customerService.getPort(SERVICE_NAME1, SalesCustomerSelect.class);
        if (isLogging()) {
            Client client = ClientProxy.getClient(customerPort);
            client.getInInterceptors().add(new LoggingInInterceptor());
            client.getOutInterceptors().add(new LoggingOutInterceptor());
        }

But not sure how to write SOAP request/response in separate log files dynamically.

Upvotes: 0

Views: 927

Answers (1)

CodeNotFound
CodeNotFound

Reputation: 1081

You could write your own custom Interceptor in which you log the SOAP request/response to their respective log file.

Looks like you are using Apache CXF. The documentation contains a section on writing custom Interceptors. In addition you can checkout following example.

Upvotes: 1

Related Questions