我想将所有肥皂请求记录到我的服务器。服务器实例是一个嵌入式码头服务器。
有没有办法设置一个处理程序来做到这一点。我可以访问 web.xml 文件
我想将所有肥皂请求记录到我的服务器。服务器实例是一个嵌入式码头服务器。
有没有办法设置一个处理程序来做到这一点。我可以访问 web.xml 文件
您将在嵌入式码头启动时需要以下内容...
(这是假设 Jetty 9)
HandlerCollection handlers = new HandlerCollection();
ContextHandlerCollection contexts = new ContextHandlerCollection();
// your context specific handlers are added to "contexts" here
server.setHandler(handlers);
NCSARequestLog requestLog = new NCSARequestLog();
requestLog.setFilename("/path/to/my/logs/yyyy_mm_dd.request.log");
requestLog.setFilenameDateFormat("yyyy_MM_dd");
requestLog.setRetainDays(90);
requestLog.setAppend(true);
requestLog.setExtended(true);
requestLog.setLogCookies(false);
requestLog.setLogTimeZone("GMT");
RequestLogHandler requestLogHandler = new RequestLogHandler();
requestLogHandler.setRequestLog(requestLog);
handlers.addHandler(requestLogHandler);
码头专家:
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all</artifactId>
<version>9.3.8.v20160314</version>
<type>pom</type>
</dependency>
代码:
NCSARequestLog requestLog = new NCSARequestLog();
requestLog.setFilename("/path/to/my/logs/yyyy_mm_dd.request.log");
requestLog.setFilenameDateFormat("yyyy_MM_dd");
requestLog.setRetainDays(90);
requestLog.setAppend(true);
requestLog.setExtended(true);
requestLog.setLogCookies(false);
requestLog.setLogTimeZone("GMT"); // or GMT+2 and so on.
server.setRequestLog(requestLog); // here will set global request log
NCSARequestLog 是同步日志,如果你想使用 log4j,这样做:
public class AccessLogHandler extends AbstractNCSARequestLog {
private Log logger = LogFactory.getLog(AccessLogHandler.class);
@Override
protected boolean isEnabled() {
return true;
}
@Override
public void write(String requestEntry) throws IOException {
logger.info(requestEntry);
}
}
用于AccessLogHandler
替换NCSARequestLog
和配置您的log4j.properties
.