Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure embedded jetty server to log all requests?

I want to log all soap requests to my server. The server instance is an embedded jetty server.

Is there a way to setup a handler to do this. I have access to the web.xml file

like image 719
sab Avatar asked Oct 18 '13 20:10

sab


People also ask

How does Jetty handle multiple requests?

There is this server port that jetty listens on and some number of acceptor threads whose job it is to get connection objects made between the client and server side.

How many connections can Jetty handle?

In HTTP/1.1, a destination holds a pool of connections (controlled by maxConnectionsPerDestination, by default 64). So if you send requests in a tight loop, assuming connection establishment takes zero time, you can have at most 64 (on the network) + 1024 (queued) outstanding requests.

Where are Jetty logs stored?

By default all jetty logs are placed into jetty/base/logs directory. The main Rhythmyx log can be found at jetty/base/logs/server.


2 Answers

You'll want the following on your embedded jetty startup...

(This is assuming 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);
like image 83
Joakim Erdfelt Avatar answered Oct 31 '22 09:10

Joakim Erdfelt


jetty maven:

   <dependency>
        <groupId>org.eclipse.jetty.aggregate</groupId>
        <artifactId>jetty-all</artifactId>
        <version>9.3.8.v20160314</version>
        <type>pom</type> 
   </dependency>

Code:

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 is sync log, if you want to use log4j, do it like that:

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

}

use AccessLogHandler to replace the NCSARequestLog and config your log4j.properties.

like image 32
user918888 Avatar answered Oct 31 '22 10:10

user918888