7

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

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
sab
  • 9,767
  • 13
  • 44
  • 51
  • If you want to do this non-programmatically (i.e. via `jetty.xml`) have a look at [the configuration in this question](https://stackoverflow.com/questions/10861869/empty-request-logs). Worked for me in Jetty 8.1.13, where the embedded code was configuring `jetty.xml` *after* creating and injecting the `HandlerCollection` to the server. – Janaka Bandara Jul 06 '19 at 14:47

2 Answers2

8

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);
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Where so I add this. I am sorry I am very new to Jetty. Can I just configure this in web.xml – sab Oct 18 '13 at 20:59
  • 2
    You said you had embedded jetty, that means you have some java code that starts your jetty server. Look for that code, search for "new Server(". Either that, or you are not using embedded jetty. – Joakim Erdfelt Oct 18 '13 at 21:01
  • I do not have access to that part of code. All I have is a way to send parameters to jvm or edit web.xml. Is there a way to do it with web.xml – sab Oct 18 '13 at 21:35
  • 1
    Not really. the web.xml is for configuring the deployment of the webapp itself. Access/Request logging is a function of the server. However, you can fake it with a custom Filter of your own to log each request, but that would be custom code in your webapp, subject to the standard restrictions of the code of a webapp. – Joakim Erdfelt Oct 18 '13 at 22:38
  • 1
    I suspect you are not using embedded jetty, but rather standalone jetty. If that's the case, you should ask your administrators to enable `etc/jetty-requestlog.xml` – Joakim Erdfelt Oct 18 '13 at 22:39
  • what does this do - "requestLog.setExtended" when set to true? – sunskin Jan 15 '14 at 15:12
  • when you initialize and add handlers to contexts (ContextHandlerCollection contexts), it's not used anywhere in below. where do I need to hook it up with server it self with ContextHanderCollection? – handicop Aug 21 '15 at 05:39
  • I had to add `server.setRequestLog(requestLog);` – Michał Niklas Dec 16 '15 at 14:46
  • @MichałNiklas the `Server.setRequestLog()` [was added for Jetty 9.3+](https://github.com/eclipse/jetty.project/commit/e3bda4efbefa2370d7d9b594aa97dc60a2b37235) – Joakim Erdfelt Dec 16 '15 at 16:37
  • After following thes instructions, I can see debug logs in my console, which is great. But I'm unable to see the logs being outputted to any files. – Janac Meena Jun 27 '16 at 18:44
3

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.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
user918888
  • 151
  • 1
  • 7