如何配置嵌入式jetty服务器以记录所有请求?

时间:2013-10-18 20:53:51

标签: java jetty embedded-jetty

我想将所有soap请求记录到我的服务器。服务器实例是嵌入式jetty服务器。

有没有办法设置处理程序来执行此操作。我可以访问web.xml文件

2 个答案:

答案 0 :(得分:6)

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

(这是假设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);

答案 1 :(得分:3)

码头maven:

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

代码:

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

}

NCSARequestLog是同步日志,如果你想使用log4j,那就这样做:

AccessLogHandler

使用NCSARequestLog替换log4j.properties并配置您的<script src="assets/js/ui-bootstrap-tpls-0.14.3.min.js"></script> <script src="assets/js/EarnfitApp.js"></script> <script src="assets/js/controllers.js"></script>