转:Enabling HTTP request response logging on JBoss 4.2

Enabling HTTP request response logging on JBoss 4.2

转自:http://blog.leenarts.net/2010/08/26/http-request-response-logging-jboss-4-2/

Today I had a bit of a fiddle with JBoss 4.2 due to IE7/IE8 compatibility differences.

When opening or downloading a file in IE7 over an SSL connection (HTTPS), IE7 requires a bit of HTTP header-fu to allow you to actually perform that action.

It’s simple really. Just add “Pragma: public” and “Cache-Control: maxage=3600″ to your HTTP headers and you’re good to go.

In Java code:

1
2
response.setHeader( "Pragma" , "public" );
response.setHeader( "Cache-Control" , "maxage=3600" );

To check if it’s working was a different matter. How to check the HTTP header content when things get sent over an HTTPS connection… FireBug doesn’t work, and so doesn’t any other developer tooling in any browser. Tracing the network traffic is pretty useless too, it’s encrypted. Last option, trace it on the server. Simply enabling network tracing on the Java VM didn’t work. For some reason JBoss doesn’t pick up this Java VM setting, probably because they are using a NIO based stack or something. I really don’t know.

JBoss does use Catalina for it’s HTTP/HTTPS/Web container handling. Catalina is another name for Tomcat. Tomcat has this valve thing which allows you to plug into the processing pipe. Good thing there are standard Valve implementations available that do just what I needed.

Just drop the next snippet somewhere appropriate:

1
2
3
4
5
<!-- Enable http request/response logging.-->
< Valve className = org .apache.catalina.valves.RequestDumperValve
            prefix = localhost_access_log . suffix=.log
            pattern = common directory=${jboss.server.home.dir}/log
            resolveHosts = false />

Appropriate in my case was in the file “[JBOSS-HOME]/server/default/deploy/jboss-web.deployer/server.xml”.

And lo and behold it logged:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
11:46:44,262 INFO  [[localhost]]           authType=BASIC
11:46:44,262 INFO  [[localhost]]      contentLength=48189
11:46:44,262 INFO  [[localhost]]        contentType=application/pdf
11:46:44,262 INFO  [[localhost]]             header=Pragma=public
11:46:44,262 INFO  [[localhost]]             header=Cache-Control=maxage=3600
11:46:44,262 INFO  [[localhost]]             header=Expires=Thu, 01 Jan 1970 01:00:00 CET
11:46:44,262 INFO  [[localhost]]             header=X-Powered-By=Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag=JBoss_4_2_3_GA date=200807181417)/JBossWeb-2.0
11:46:44,332 INFO  [[localhost]]             header=X-Powered-By=JSF/1.2
11:46:44,332 INFO  [[localhost]]             header=X-Powered-By=Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag=JBoss_4_2_3_GA date=200807181417)/JBossWeb-2.0
11:46:44,332 INFO  [[localhost]]             header=X-Powered-By=JSF/1.2
11:46:44,332 INFO  [[localhost]]             header=Content-Type=application/pdf
11:46:44,332 INFO  [[localhost]]             header=Content-Length=48189
11:46:44,332 INFO  [[localhost]]             header=Date=Thu, 26 Aug 2010 09:46:44 GMT
11:46:44,332 INFO  [[localhost]]            message=null
11:46:44,332 INFO  [[localhost]]         remoteUser=admin
11:46:44,332 INFO  [[localhost]]             status=200

补充参数说明:

其中具体配置参数如下所示:

 

className

实现的Java类名。必须被设置成org.apache.catalina.valves.AccessLogValve。

directory

存放日志文件的目录,可以是相对路径或者绝对路径。如果使用相对路径,是指相对于 $CATALINA_HOME的路径。如果不指定directory属性,缺省值是“logs”(相对于 $CATALINA_HOME)

pattern

需要记录的请求/响应不同信息域的格式布局。如果是“common”或者“combine”,说明选择标准格式。下面会有关于配置这个属性的更多信息。

prefix

日志文件名的前缀。如果没有指定,缺省值是“access_log.”。如果不想使用前缀,使用长度为0的字符串。

 

resolveHosts

将远端主机的IP地址通过DNS查询转换成主机名,设为true。如果为false,忽略DNS查询,报告远端的IP地址。

 

suffix

日志文件名的后缀。如果没有指定,缺省值是“”。如果不想使用后缀,使用长度为0的字符串。

 

rotatable

缺省值为true,用来决定日志是否翻转的标志。如果为false,日志文件永远不翻转,并且忽略fileDataFormat。要谨慎使用。

 

condition

打开条件日志。如果设置了这个属性,只有在ServletRequest.getAttribute()是null的时候,才会为请求创建日志。比如,如果condition设为junk,则只有在Servlet.getAttribute("junk")==null的时候,才会记录这个请求。使用过滤器,可以很容易设置(或者取消设置)不同请求的属性。

fileDateFormat 允许在日志文件名称中使用定制的日期格式。日志的格式也决定了日志文件翻转的频率。如果想每个小时翻转一次,将这个值设为yyyy-MM-dd.HH

 

pattern属性值由字符串常量和pattern标识符加上前缀"%"组合而成。pattern标识符加上前缀"%",用来代替当前请求/响应中的对应的变量值。目前支持如下的pattern:

  • %a - 远端IP地址
  • %A - 本地IP地址
  • %b - 发送的字节数,不包括HTTP头,如果为0,使用"-"
  • %B - 发送的字节数,不包括HTTP头
  • %h - 远端主机名(如果resolveHost=false,远端的IP地址)
  • %H - 请求协议
  • %l - 从identd返回的远端逻辑用户名(总是返回 '-')
  • %m - 请求的方法(GET,POST,等)
  • %p - 收到请求的本地端口号
  • %q - 查询字符串(如果存在,以 '?'开始)
  • %r - 请求的第一行,包含了请求的方法和URI
  • %s - 响应的状态码
  • %S - 用户的session ID
  • %t - 日志和时间,使用通常的Log格式
  • %u - 认证以后的远端用户(如果存在的话,否则为'-')
  • %U - 请求的URI路径
  • %v - 本地服务器的名称
  • %D - 处理请求的时间,以毫秒为单位
  • %T - 处理请求的时间,以秒为单位

举例:
<Valve className="org.apache.catalina.valves.AccessLogValve"
        prefix="localhost_access_log." suffix=".log"
        pattern="%h %l %u %t %r %s %b"
        timestamp="true"
        directory="${jboss.server.home.dir}/com/log" resolveHosts="true" />

猜你喜欢

转载自liu-g927.iteye.com/blog/1506559