Gradle run with Jetty access request log

Introduction

The server access log records all incoming requests processed by the server. There’s one entry per request received, in the standard NCSA format. This will allow you to use standard tool like Webalizer to analyze these log files.

A typical request log entry includes the remote host/IP address, date, method, URL, result, size, referrer and user agent. For example:

128.208.24.193 - - [2015-06-04 20:39:21,382] 
"GET /orderprocessor/healthcheck/1234"
200 9197 124 "ELB-HealthChecker/1.0"
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040614 Firefox/0.8"

Jetty provides a request log implementation called NCSARequestLog which supports the NCSA format in files that can be rolled over on a daily basis.
To turn it on, you just need to edit webapp/WEB-INF/jetty-web.xml

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath">/myweb</Set>
    <Set name="handler">
        <New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler">
            <Set name="requestLog">
                <New id="RequestLogImpl" class="org.eclipse.jetty.server.NCSARequestLog">
                    <Set name="filename">/tools/logs/access.log</Set>
                    <Set name="filenameDateFormat">yyyy_MM_dd</Set>
                    <Set name="LogTimeZone">GMT</Set>
                    <Set name="retainDays">1</Set>
                    <Set name="append">true</Set>
                </New>
            </Set>
        </New>
    </Set>
</Configure>

Alternatively, you could also update your gradle build file

task jettyRunWar(dependsOn: ['war', ":DBMigration:<wbr />runMigration"]) << {
    ant.jettyRun(jettyPort: project.ext.jettyPort, daemon: false, 
   requestLog: "org.eclipse.jetty.server.<wbr />NCSARequestLog" ) 
   {webApp(war: war.archivePath)}
}

You should see access log printed out on the console.

(Visited 39 times, 1 visits today)

Leave a Reply