GlassFish does allow one to specify the access log format, but how this works is not obvious. If one tries to create a formatting string, which should result in one of the Apache access log formats, the resulting output does contain all the specified fields and in the right order, but the field delimiters are not preserved from the formatting string and instead all the fields are quoted and separated by spaces. That's not quite what we want, especially if you plan to feed the logs into a log analyzer that expect the usual Apache syntax.
While getting Confluence wiki to run on GlassFish v3, I fetched the GF source code and since I already had it, I thought that it should be trivial to find out how the Access log format gets processed in GF.
To my big surprise, I found out that there are classes with very suspicious names:
CommonAccessLogFormatterImpl
, CombinedAccessLogFormatterImpl
and DefaultAccessLogFormatterImpl
. A minute later I also found this piece of code "hidden" in PEAccessLogValve
:
// Predefined patterns private static final String COMMON_PATTERN = "common"; private static final String COMBINED_PATTERN = "combined"; ... ... /** * Set the format pattern, first translating any recognized alias. * * @param p The new pattern */ public void setPattern(String p) { if (COMMON_PATTERN.equalsIgnoreCase(p)) { formatter = new CommonAccessLogFormatterImpl(); } else if (COMBINED_PATTERN.equalsIgnoreCase(p)) { formatter = new CombinedAccessLogFormatterImpl(); } else { formatter = new DefaultAccessLogFormatterImpl(p, getContainer()); } }
Whoa! So both Apache formats are implemented already and one just needs to know how to "unlock" them. The "common" and "combined" constants looked like the magic keywords to do just that, and sure enough, when one sets either of them as the formatting string, the log will contain the expected output.
You can also use
asadmin
to make this config change:
asadmin set server.http-service.access-log.format="combined"
After a restart the log now uses the requested format:
0:0:0:0:0:0:0:1%0 - - [21/Dec/2009:07:42:45 -0800] "GET /s/1722/3/_/images/icons/star_grey.gif HTTP/1.1" 304 0 0:0:0:0:0:0:0:1%0 - - [21/Dec/2009:07:42:45 -0800] "GET /images/icons/add_space_32.gif HTTP/1.1" 304 0 0:0:0:0:0:0:0:1%0 - - [21/Dec/2009:07:42:45 -0800] "GET /images/icons/feed_wizard.gif HTTP/1.1" 304 0 0:0:0:0:0:0:0:1%0 - - [21/Dec/2009:07:42:45 -0800] "GET /images/icons/people_directory_32.gif HTTP/1.1" 304 0 0:0:0:0:0:0:0:1%0 - - [21/Dec/2009:07:42:45 -0800] "GET /s/1722/3/_/images/icons/add_12.gif HTTP/1.1" 304 0
Believe it or not, this information is not documented anywhere in the official documentation, and even folks Matthew chatted with on Sun's internal support mailing lists had no clue about it. Ideally the GF documentation and UI should be updated to make changing the access log format as simple as it should be.
Oh btw, open source FTW, when documentation is lacking one can at least read the sources!