需要使用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请求/响应。