DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Setting up Request Rate Limiting With NGINX Ingress
  • Rocket.rs API Demo Implementing Header-Based API Key Protection
  • Validate XML Request Against XML Schema in Mule 4
  • Mitigate the Risks of Poor Performing Cloud Apps

Trending

  • Decoding the Differences: Continuous Integration, Delivery and Deployment
  • Continuous Integration vs. Continuous Deployment
  • Using Open Source for Data Integration and Automated Synchronizations
  • Message Construction: Enhancing Enterprise Integration Patterns

Ratpacked: Request Logging

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

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
Nov. 01, 15 · Tutorial
Like (3)
Save
Tweet
Share
3.33K Views

Join the DZone community and get the full member experience.

Join For Free

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!'
        }

    }

}
Requests

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

Opinions expressed by DZone contributors are their own.

Related

  • Setting up Request Rate Limiting With NGINX Ingress
  • Rocket.rs API Demo Implementing Header-Based API Key Protection
  • Validate XML Request Against XML Schema in Mule 4
  • Mitigate the Risks of Poor Performing Cloud Apps

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: