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();
}
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With