Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded Jetty (Access) Request Logging

I am working on creating some rest API using JAX-RS and embedded Jetty.I Enable Server logging by adding LogRequestHandler in Server.java file.

Question is why jetty is writing 200 for every request 0:0:0:0:0:0:0:1 - - [03/Nov/2016:16:59:57 +0500] "GET /app/check HTTP/1.1" 200 - 4

though check end point is not implemented not exists in the app.

source code:

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    //context.setContextPath("/");

    ResourceConfig config = new ResourceConfig();
    config.packages("server");
    ServletHolder servlet = new ServletHolder(new ServletContainer(config));
    context.addServlet(servlet,"/*");

    NCSARequestLog requestLog = new NCSARequestLog("/var/logs/jetty/log-yyyy_mm_dd.request.log");
    requestLog.setAppend(true);
    requestLog.setExtended(false);
    requestLog.setLogTimeZone("GMT+5");
    requestLog.setLogLatency(true);
    requestLog.setRetainDays(90);
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    requestLogHandler.setRequestLog(requestLog);

    HandlerList topLevelHandlers = new HandlerList();
    topLevelHandlers.addHandler(context);
    topLevelHandlers.addHandler(requestLogHandler);

    try {
        jettyServer.setHandler(topLevelHandlers);
        jettyServer.dumpStdErr();
        jettyServer.start();
    }
like image 750
Tahir Ali Avatar asked Nov 03 '16 12:11

Tahir Ali


2 Answers

Updated Answer

Do not use RequestLogHandler it is now deprecated.

Any RequestLog interface implementation should be set on the Server.setRequestLog(RequestLog) method now.

This will catch more requests, even 400 (Bad Request), requests that fail parsing, requests that don't belong to a context, and even requests that are not dispatched to the handler tree.

Original Answer

The RequestLogHandler should be executed before any context its tracking.

The ideal way to use RequestLog is to set it on the server and not use the RequestLogHandler ...

jettyServer.setRequestLog(requestLog);

But if you have a need to use it as a Handler, then you should make sure it executes before any contexts you want to track ...

HandlerList topLevelHandlers = new HandlerList();
topLevelHandlers.addHandler(requestLogHandler);
topLevelHandlers.addHandler(context);
jettyServer.setHandler(topLevelHandlers);

or wrap the context with the request log handler ...

requestLogHandler.setHandler(context);
jettyServer.setHandler(requestLogHandler);
like image 176
Joakim Erdfelt Avatar answered Oct 12 '22 09:10

Joakim Erdfelt


Fix this by re-arranging the handler in the following order

I set resource handler as handlers for the request log (ie it is the outer handler) then the requestlog handler is added to the main jetty handlers. Then requests would go to request log -> handlers -> request log and back to jetty

like image 30
Tahir Ali Avatar answered Oct 12 '22 09:10

Tahir Ali