如何使用log4j2在单独的日志文件中动态记录SOAP请求/响应

时间:2019-05-29 08:46:27

标签: soap cxf log4j2

需要使用log4j2编写用于SOAP请求和响应的基于apikey的单独日志文件。我们是客户端还是使用SOAP Web服务。

我能够基于api键为REST请求和响应编写单独的日志文件,但不确定如何对SOAP请求和响应进行操作

在单独的日志文件中写入日志的代码如下

需要使用log4j2编写用于SOAP请求和响应的基于apikey的单独日志文件。我们是客户端还是使用SOAP Web服务。

我能够基于api键为REST请求和响应编写单独的日志文件,但不确定如何对SOAP请求和响应进行操作

在单独的日志文件中写入日志的代码如下:

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;
    }
}

用于将SOAP请求/响应写入文件的代码如下:

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());
        }

但是不确定如何在单独的日志文件中动态编写SOAP请求/响应。

1 个答案:

答案 0 :(得分:0)

您可以编写自己的自定义Interceptor,在其中将SOAP请求/响应记录到它们各自的日志文件中。

好像您正在使用Apache CXF。该文档包含section on writing custom Interceptors。另外,您可以在example之后结帐。

相关问题