Module io.jooby

Class AccessLogHandler

java.lang.Object
io.jooby.handler.AccessLogHandler
All Implemented Interfaces:
Route.Aware, Route.Filter

public class AccessLogHandler extends Object implements Route.Filter
Access Log Handler.

Log incoming requested using the NCSA format (a.k.a common log format). If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header, please consider to set Router.setTrustProxy(boolean) option.

usage


 {
   use(new AccessLogHandler());

   ...
 }
 

Output looks like:

 127.0.0.1 - - [04/Oct/2016:17:51:42 +0000] "GET / HTTP/1.1" 200 2
 

You probably want to configure the AccessLogHandler logger to save output into a new file:

 <appender name="ACCESS" class="ch.qos.logback.core.rolling.RollingFileAppender">
   <file>access.log</file>
   <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
     <fileNamePattern>access.%d{yyyy-MM-dd}.log</fileNamePattern>
   </rollingPolicy>

   <encoder>
     <pattern>%msg%n</pattern>
   </encoder>
 </appender>

 <logger name="io.jooby.AccessLogHandler" additivity="false">
   <appender-ref ref="ACCESS" />
 </logger>
 

By defaults it log the available user context: Context.getUser(). To override this:


 {

   use("*", new AccessLogHandler(ctx -> {
     // retrieve user ID from context.
   }));
 }
 

custom log function

By default it uses the underlying logging system: logback. That's why we previously show how to configure the io.jooby.AccessLogHandler in logback.xml.

If you want to log somewhere else and/or use a different method then:


 {
   use("*", new AccessLogHandler()
     .log(line -> {
       System.out.println(line);
     }));
 }
 

This is just an example but of course you can log the NCSA line to database, jms queue, etc...

latency


 {
   use("*", new RequestLogger()
       .latency());
 }
 

It add a new entry at the last of the NCSA output that represents the number of ms it took to process the incoming release.

request and response headers

You can add extra headers using the requestHeader(String...) and responseHeader(String...)

dateFormatter


 {
   use("*", new RequestLogger()
       .dateFormatter(ts -> ...));

   // OR
   use("*", new RequestLogger()
       .dateFormatter(DateTimeFormatter...));
 }
 

You can provide a function or an instance of DateTimeFormatter.

The default formatter use the default server time zone, provided by ZoneId.systemDefault(). It's possible to just override the time zone (not the entirely formatter) too:


 {
   use("*", new RequestLogger()
      .dateFormatter(ZoneId.of("UTC"));
 }
 
Since:
2.5.2
Author:
edgar
  • Constructor Details

    • AccessLogHandler

      public AccessLogHandler(@NonNull Function<Context,String> userId)
      Creates a new AccessLogHandler and use the given function and userId provider. Please note, if the user isn't present this function is allowed to returns - (dash character).
      Parameters:
      userId - User ID provider.
    • AccessLogHandler

      public AccessLogHandler()
      Creates a new AccessLogHandler without user identifier.
  • Method Details

    • apply

      @NonNull public Route.Handler apply(@NonNull Route.Handler next)
      Description copied from interface: Route.Filter
      Chain the filter within next handler.
      Specified by:
      apply in interface Route.Filter
      Parameters:
      next - Next handler.
      Returns:
      A new handler.
    • log

      @NonNull public AccessLogHandler log(@NonNull Consumer<String> log)
      Log an NCSA line to somewhere.
      
       {
         use("*", new RequestLogger()
             .log(System.out::println)
         );
       }
       
      Parameters:
      log - Log callback.
      Returns:
      This instance.
    • dateFormatter

      @NonNull public AccessLogHandler dateFormatter(@NonNull DateTimeFormatter formatter)
      Override the default date formatter.
      Parameters:
      formatter - New formatter to use.
      Returns:
      This instance.
    • dateFormatter

      @NonNull public AccessLogHandler dateFormatter(Function<Long,String> formatter)
      Override the default date formatter.
      Parameters:
      formatter - New formatter to use.
      Returns:
      This instance.
    • dateFormatter

      @NonNull public AccessLogHandler dateFormatter(@NonNull ZoneId zoneId)
      Keep the default formatter but use the provided timezone.
      Parameters:
      zoneId - Zone id.
      Returns:
      This instance.
    • extended

      @NonNull public AccessLogHandler extended()
      Append Referer and User-Agent entries to the NCSA line.
      Returns:
      This instance.
    • requestHeader

      @NonNull public AccessLogHandler requestHeader(@NonNull String... names)
      Append request headers to the end of line.
      Parameters:
      names - Header names.
      Returns:
      This instance.
    • responseHeader

      @NonNull public AccessLogHandler responseHeader(@NonNull String... names)
      Append response headers to the end of line.
      Parameters:
      names - Header names.
      Returns:
      This instance.