-
User guide
- Part 1 - Introduction
- Part 2 - Core Restlet
- Part 3 - Restlet Editions
- Part 4 - Restlet Extensions
- Appendices
- Tutorials
- Javadocs
- Change Log
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:
- Date (YYYY-MM-DD)
- Time (HH:MM:SS)
- Client address (IP)
- Remote user identifier (see RFC 1413)
- Server address (IP)
- Server port
- Method (GET, POST…)
- Resource reference path (including the leading slash)
- Resource reference query (excluding the leading question mark)
- Response status code
- Number of bytes sent
- Number of bytes received
- Time to serve the request (in milliseconds)
- Host reference
- Client agent name
- 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:
- Client address (IP)
- Remote user identifier (see RFC 1413)
- Server address (IP) and port
- Method (GET, POST…)
- Resource reference path (including the leading slash)
- Resource reference query (excluding the leading question mark)
- Response status code
- Number of bytes sent
- Number of bytes received
- Host reference
- Client agent name
- 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.