Thanks to Grizzly contributor, Pier Fumagalli, a long standing issue to implement access logging has been closed.
Pier’s implementation is based off Apache’s mod_log_config and thus is probably familiar to a lot of developers.
Getting Started
Logging is enabled via a Probe that may be registered with one or more NetworkListeners. It is recommended to use the org.glassfish.grizzly.http.httpserver.accesslog.AccessLogBuilder to create instances of the probe.
For example:
|
1 2 |
final AccessLogBuilder builder = new AccessLogBuilder("/tmp/access.log"); builder.instrument(httpServer.getServerConfiguration()); |
The above code registers the default access logging probe globally to all listeners
associated with the httpServer. However, the following configuration options
are available through the builder:
| format (either type String or AccessLogFormat (described later)) | Specifies the log record format. If this configuration is not explicitly set, it will default to the NCSA extended/combined log format. Additional formats are available. See the javadocs for ApacheLogFormat for details. |
| timeZone (either type String or java.util.TimeZone) | The time zone for the timestamped log records. If not specified, it will default to the timezone of the system running the server. |
| statusThreshold (type int) | Specifies the minimum HTTP status code that will trigger an entry in the access log. If not specified, then all status codes are valid. |
| rotateHourly | If set, then the access logs will be rotated hourly. For example, if the file name specified was access.log, files will be archived on a hourly basis with names like access-yyyyMMDDhh.log |
| rotateDaily | If set, then the access logs will be rotated daily. For example, if the file name specified was access.log, files will be archived on a daily basis with names like access-yyyyMMDD.log |
| rotatePattern (type String) | Specifies a java.util.SimpleDateFormat pattern for automatic log file rotation. For example, if the file name specified was access.log and the rotation patternspecified was EEE (day name in week), files will be archived on a daily basis with names likeaccess-Mon.log, access-Tue.log, etc. |
| synchronous (type boolean) | Specifies whether access log entries should be written
|
Custom Log Formatting and Appending
Log recorder formatting and appending is accomplished by the AccessLogFormat
and AccessLogAppender interfaces.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package org.glassfish.grizzly.http.server.accesslog; ... /** * An interface defining a component capable of formatting {@link Response}s * into printable <em>access log entries</em>. * * <p>Implementations of this class <b>must</b> be thread-safe.</p> */ public interface AccessLogFormat { /** * Format the data contained in the specified {@link Response} and return * a {@link String} which can be appended to an access log file. * * @param response The {@link Response} holding the data to format. * @param timeStamp The {@link Date} at which the request was originated. * @param responseNanos The time, in nanoseconds, the {@link Response} * took to complete. */ String format(Response response, Date timeStamp, long responseNanos); } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package org.glassfish.grizzly.http.server.accesslog; ... /** * An interface defining an <em>appender</em> for Grizzly access logs entries. */ public interface AccessLogAppender extends Closeable { /** * Append the specified access log entry. * * @param accessLogEntry The {@link String} value of the data to be append * in the access log. * @throws IOException If an I/O error occurred appending to the log. */ void append(String accessLogEntry) throws IOException; /** * Close any underlying resource owned by this appender. */ @Override void close() throws IOException; } |
Grizzly’s implementation, as hinted by the builder documentation above, does
come with defaults for both interfaces.
The default AccessLogFormatter is the ApacheLogFormatter. This implementation
provides the same functionality as the mod_log_config (<http://httpd.apache.org/docs/2.2/mod/mod_log_config.html>).
Custom formats may be created using the format Strings as specified by mod_log_config.
The ApacheLogFormatter also defines several default format Strings that may be used –
see the javadocs for details.
As far as AccessLogAppenders, the following are included:
- FileAppender (standard log file writing)
- QueueingAppender (asyncronous log file writing)
- RotatingFileAppender (automatic log file rotation)
- StreamAppender (appends to the provided java.io.OutputStream)
Please feel free to give the access logging a shot in either the 2.3 or 3.0 nightly builds and let us know if you find any issues or have ideas for improvement.
A big thanks to Pier for taking the time to contribute back to the community!