Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Ratpacked: Request Logging

DZone's Guide to

Ratpacked: Request Logging

Ratpack has a special log request handler. Here's a request logging tutorial, featuring a Groovy DSL definition.

Free Resource

Discover 50 of the latest mobile performance statistics with the Ultimate Guide to Digital Experience Monitoring, brought to you in partnership with Catchpoint.

Ratpack comes with a special handler to log requests in the common log or NCSA format. We need the (default) interfaceratpack.handling.RequestLogger which has a method ncsa that returns a handler instance capable of logging our request data.

In the following example code we have a Groovy DSL definition of our Ratpack application. We use the all method to add the request logger. The RequestLogger implementation will make sure the complete handler chain is finished before logging the information.

import ratpack.handling.RequestLogger

import static ratpack.groovy.Groovy.ratpack

ratpack {

    handlers {

        // Here we add the request logger 
        // for logging our request information
        // in common log or NCSA format.
        all(RequestLogger.ncsa())

        get {
            render 'Ratpack rules!'
        }

    }

}

When we run our application with logging enabled we get the following sample output when we request the / path of our application:

[ratpack-compute-5-16] INFO ratpack.request - 0:0:0:0:0:0:0:1 - - [12/Oct/2015:08:57:59 +0200] "GET / HTTP/1.1" 200 - id=133aef4a-2abe-acc0-90a3-783826bad515

We see the request method, path and status code and we see Ratpack adds a unique request identifier to our requests and it is also logged with our request logger. The default logger name is ratpack.request. We can use this to for example redirect the request logging to a file using the configuration of a SLF4J logging implementation. To change the logger implementation we can give our own as an argument for the ncsa method:

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import ratpack.handling.RequestLogger

import static ratpack.groovy.Groovy.ratpack

ratpack {
    
    // Create custom Logger implementation for request logging.
    final Logger requestLogger = LoggerFactory.getLogger('com.mrhaki.sample.requestLogger')

    handlers {

        // Instruct NCSA request logger to use 
        // our requestLogger Logger implementation.
        all(RequestLogger.ncsa(requestLogger))

        get {
            render 'Ratpack rules!'
        }

    }

}

Is your APM strategy broken? This ebook explores the latest in Gartner research to help you learn how to close the end-user experience gap in APM, brought to you in partnership with Catchpoint.

Topics:
performance ,ratpacked ,logging ,groovy

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}