Log service

Introduction

The log service provides access to logging service. The implementation is fully based on the standard logging mechanism introduced in JDK 1.4.

Being able to properly log the activity of a Web application is a common requirement. Restlet Components know by default how to generate Apache-like logs or even custom ones. By taking advantage of the logging facility built in the JDK, the logger can be configured like any standard JDK log to filter messages, reformat them or specify where to send them. Rotation of logs is also supported; see the java.util.logging package for details.

Note that you can customize the logger name given to the java.util.logging framework by modifying the Component’s “logService” property. In order to fully configure the logging, you need to declare a configuration file by setting a system property like:

System.setProperty("java.util.logging.config.file", "/your/path/logging.config"); 

For details on the configuration file format, please check the JDK’s LogManager class. You can also have a look at the Restlet 2.4 logging documentation.

Default access log format

The default format follows the W3C Extended Log File Format with the following fields used:

  1. Date (YYYY-MM-DD)
  2. Time (HH:MM:SS)
  3. Client address (IP)
  4. Remote user identifier (see RFC 1413)
  5. Server address (IP)
  6. Server port
  7. Method (GET, POST…)
  8. Resource reference path (including the leading slash)
  9. Resource reference query (excluding the leading question mark)
  10. Response status code
  11. Number of bytes sent
  12. Number of bytes received
  13. Time to serve the request (in milliseconds)
  14. Host reference
  15. Client agent name
  16. Referrer reference

If you use Analog to generate your log reports, and if you use the default log format, then you can simply specify this string as a value of the LOGFORMAT command: (%Y-%m-%d\t%h:%n:%j\t%S\t%u\t%j\t%j\t%j\t%r\t%q\t%c\t%b\t%j\t%T\t%v\t%B\t%f)

How to customize the access log format

First method: customize the log format

You can update the log format by setting the “responseLogFormat” attribute of the logging service. This is a simple template which leverages the variables listed here Template.

Here is a sample template and a sample log trace.

Component c = new Component();
c.getServers().add(Protocol.HTTP, 8182);
c.getDefaultHost().attach(new MyApplication());

c.getLogService().setResponseLogFormat("{ciua} {cri} {ra} {m} {rp} {rq} {S} {ES} {es} {hh} {cig} {fi}");

c.start();

This log format displays the following data:

  1. Client address (IP)
  2. Remote user identifier (see RFC 1413)
  3. Server address (IP) and port
  4. Method (GET, POST…)
  5. Resource reference path (including the leading slash)
  6. Resource reference query (excluding the leading question mark)
  7. Response status code
  8. Number of bytes sent
  9. Number of bytes received
  10. Host reference
  11. Client agent name
  12. Referrer reference

For example:

127.0.0.1  localhost:8182 GET /ping  200 5 0 http://localhost:8182 curl/7.35.0 http://test.example.com

Second method: write the log manually

As you may have noticed, the log template does not handle data such as the response time, the date of reception of the request. In this case, you can programmatically write your own log trace. In order to do so, just inherit from the LogService class and override the getResponseLogMessage(Response, int) method. The first parameter is the Response object (wich gives your access to the Request object), and the duration of the call.