Saturday, July 3, 2010

logging more information in http logs in weblogic

Till recently I had not had occasion to actually use the http access.log in weblogic. I usually am able to use ethereal or whatever to look at the bits across the wire or httwatch and recently have been using firebug
But if you want WL to generate more detailed http logs....
This is where I ran into a little block, its not as simple as changing the access log format to "extended" in the console. You need to do that first and also WRITE A CLASS to do the extended logging. crazy I wonder why they did that.

access.log in weblogic: by default logs the HTTP method URI the status code and not much else. I needed to see the cookies coming in with the request and this was a production machine so no possibility of attaching the debugger.

As you know its a W3C specified common logging format for HTTP requests across the web. If you are interested take a look at the spec.

For some reason Weblogic decided to limit their default to very little information and in order to provide extended logging, you have to write a Class ... ie., if you want to access anything from the incoming http request that is from the http header like referrer, cookies, browserid useragent etc.,

Its pretty straight-forward.
  • The Java class must implement weblogic.servlet.logging.CustomELFLogger, which has a logField(HttpAccountingInfo, FormatStringBuffer)
  • Put the class into some directory and update the startweblogic.cmd classpath
  • Then modify the config.xml to add the high-lighted xml to the server

<server>
<name>AdminServer</name>
<ssl>
<enabled>
true</enabled>
<listen-port>8843</listen-port>
</ssl>
<web-server>
<web-server-log>
<file-name>logs/modifiedaccess.log</file-name>
<elf-fields>date x-MyCustomField cs-uri time cs-method cs-uri sc-status</elf-fields>
<log-file-format>extended</log-file-format>
<log-time-in-gmt>true</log-time-in-gmt><br />
</web-server-log>
</web-server>
<listen-address></listen-address><br /></server>

Here is the class, I just put it in the default package

import weblogic.servlet.logging.CustomELFLogger;
import weblogic.servlet.logging.FormatStringBuffer;
import weblogic.servlet.logging.HttpAccountingInfo;

/* This example outputs the User-Agent field into a
custom field called MyCustomField
*/

public class MyCustomField implements CustomELFLogger{

public void logField(HttpAccountingInfo metrics,
FormatStringBuffer buff) {
buff.appendValueOrDash(metrics.getHeader("User-Agent"));
}

static {
System.out.println("HELLO!!!!! I am a custom efield logger being picked up!");
}
}

No comments:

Post a Comment