6

我想将所有肥皂请求记录到我的服务器。服务器实例是一个嵌入式码头服务器。

有没有办法设置一个处理程序来做到这一点。我可以访问 web.xml 文件

4

2 回答 2

8

您将在嵌入式码头启动时需要以下内容...

(这是假设 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);
于 2013-10-18T20:57:44.053 回答
3

码头专家:

   <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.

于 2016-08-05T07:15:30.673 回答