Interface RequestLogger

  • All Superinterfaces:
    Handler

    public interface RequestLogger
    extends Handler
    A handler that logs information about the request.

    Implementations need only implement the log(RequestOutcome) method. This interface provides a default implementation of handle() that calls the log() method before delegating to the next handler. The of(Action) can also be used to create a request logger.

    Unless there is a good reason not to, loggers should log to LOGGER at the “info” logging level. How this logging manifests can then be controlled by configuring the logging subsystem in use.

    
     import ratpack.core.handling.RequestLogger;
     import ratpack.test.embed.EmbeddedApp;
     import static org.junit.jupiter.api.Assertions.*;
    
     public class Example {
       public static void main(String... args) throws Exception {
         EmbeddedApp.fromHandlers(c -> c
           .all(RequestLogger.ncsa())
           .all(ctx -> ctx.render("ok"))
         ).test(httpClient -> {
           assertEquals("ok", httpClient.get().getBody().getText());
         });
       }
     }
     
    See Also:
    ncsa(), RequestId, UserId
    • Method Detail

      • of

        static RequestLogger of​(Action<? super RequestOutcome> action)
        Creates a request logger with the given action as the implementation of the log(RequestOutcome) method.
        
         import ratpack.core.handling.RequestLogger;
         import ratpack.test.embed.EmbeddedApp;
         import static org.junit.jupiter.api.Assertions.*;
        
         public class Example {
           public static void main(String... args) throws Exception {
             EmbeddedApp.fromHandlers(c -> c
               .all(RequestLogger.of(outcome ->
                 RequestLogger.LOGGER.info(outcome.getRequest().getUri())
               ))
               .all(ctx -> ctx.render("ok"))
             ).test(httpClient -> {
               assertEquals("ok", httpClient.get().getBody().getText());
             });
           }
         }
         

        Unless there is reason not to, the action should log at info level to LOGGER.

        Parameters:
        action - an action that logs information about the request
        Returns:
        a request logger implementation
      • ncsa

        static RequestLogger ncsa​(org.slf4j.Logger logger)
        Logs in the NCSA Common Log format. The format for the request log is "host rfc931 username date:time request statuscode bytes" as defined by the NCSA Common (access logs) format (see link). However, if the RequestId is additionally being added to requests, the value of the request Id will be appended to the end of the request log in the form: id=requestId The resulting format is thus: "host rfc931 username date:time request statuscode bytes id=requestId"
        Parameters:
        logger - the logger to log to, at INFO level
        Returns:
        a new request logger
        See Also:
        NCSA Common Log format documentation.