sab
sab

Reputation: 9977

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

Upvotes: 7

Views: 13373

Answers (2)

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49452

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

Jetty 12

You'll be using ...

  1. The CustomRequestLog (which has syntax for it's output)
  2. An instance of RequestLog.Writer to specify where that output will go.
  3. Setting the Server.setRequestLog(RequestLog) with your configuration.

You will not use the old RequestLogHandler from Jetty 9 days, as that class was incapable of logging any requests that did not dispatch through that handler. (eg: errors, bad requests, requests without paths, requests that don't match a configured context path, etc)

Both of the below examples are snippets from the jetty-example project at:

Example having a Writer that outputs to slf4j logging at an arbitrarily configured logger name

Slf4jRequestLogWriter requestLoggingWriter = new Slf4jRequestLogWriter();
requestLoggingWriter.setLoggerName("examples.requests");

RequestLog requestLog = new CustomRequestLog(requestLoggingWriter,
    CustomRequestLog.EXTENDED_NCSA_FORMAT);

server.setRequestLog(requestLog);

You can use your logging library of choice (just have slf4j route to your logging library), and output those logging events anywhere your logging library supports (file w/overwrite, file w/append, file w/rollover, email, database, unix system logs, etc)

Example having a Writer that outputs to file.

AsyncRequestLogWriter requestLogWriter = new AsyncRequestLogWriter();
requestLogWriter.setAppend(true);
requestLogWriter.setFilename("/var/web/logs/request.log");
requestLogWriter.setRetainDays(1);

RequestLog requestLog = new CustomRequestLog(requestLogWriter,
    CustomRequestLog.EXTENDED_NCSA_FORMAT);

server.setRequestLog(requestLog);

Original Answer (Oct 18, 2013)

This is for 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);

Upvotes: 7

user918888
user918888

Reputation: 158

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.

Upvotes: 2

Related Questions