Embedded Jetty Request Logging (Access)

I am working on creating some leisure API using JAX-RS and built-in Jetty.I Enable logging by adding LogRequestHandler to the Server.java file.

The question is why the berth records 200 for each request 0:0:0:0:0:0:0:1 - - [03/Nov/2016:16:59:57 +0500] "GET /app/check HTTP/1.1" 200 - 4

although the validation endpoint is not implemented in the application.

source:

  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(); } 
+5
source share
2 answers

Fix this by reinstalling the handler in the following order

I set the resource handler as handlers for the request log (i.e., it is an external handler), then the requestlog handler is added to the main berth handlers. Then the requests are sent to the log request β†’ handlers β†’ the query log and back to the marina

+2
source

RequestLogHandler must be executed before any tracking context.

The ideal way to use RequestLog is to install it on the server, rather than using RequestLogHandler ...

 jettyServer.setRequestLog(requestLog); 

But if you need to use it as a handler, then you need to make sure that it is executed before any contexts that you want to track ...

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

or wrap the context with a query log handler ...

 requestLogHandler.setHandler(context); jettyServer.setHandler(requestLogHandler); 
+3
source

Source: https://habr.com/ru/post/1259214/


All Articles