HTTP Access Logging
HTTP Requests/Responses are logged in the access log(s) and are helpful to monitor your application's usage activities, such as the persons who access it, how many hits it receives, what the errors are etc. This information is useful for troubleshooting. As the runtime of WSO2 products are based on Apache Tomcat, you can use the Access_Log_Valve
variable in Tomcat 7 as explained below to configure HTTP access logs in WSO2 products. In addition, you can customize the access logs based on the supported Access Log Valve attributes.
Configuring HTTP access logging
To configure HTTP access logs:
Edit the
Access_Log_Valve
variable as follows in the <PRODUCT_HOME>/repository/conf/tomcat/catalina-server.xml
file, which is the server descriptor file for the embedded Tomcat integration.<Valve className="org.apache.catalina.valves.AccessLogValve" directory="${carbon.home}/repository/logs" prefix="localhost_access_log_sample." suffix=".log" pattern="%{xxx}i %{xxx}o" resolveHosts="false"/>
The pattern attribute defines the formatting layout, which consists of the information fields from the requests and responses that should be logged. The description of the formatting layout specified in the above
pattern
attribute is given below. For more example configurations, see Customizing access logs by pattern.xxx -
header name we need to log%{xxx}i -
incoming request headers%{xxx}o -
outgoing response headers
For more information on all possible values that can be given in the
pattern
parameter, go to:
http://tomcat.apache.org/tomcat-7.0-doc/config/valve.html#Access_Log_ValveIf you do not wish to customize the formatting layout, you can simply use the word 'common' or 'combined' for the
pattern
attribute to select a standard format.common - %h %l %u %t "%r" %s %b
combined - %h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"
The optimized access only supports 'common' and 'combined' as the value for this attribute.
Restart the server. This will create a log file named
localhost_access_log_sample.{DATE}.log
inside the <PRODUCT_HOME>/repository/logs
directory.
Customizing access logs by pattern
Given below are a few sample configurations, for ways in which the access logs can be customized by the pattern
attribute:
Example 1: Logging request headers
The configuration is as follows:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="${carbon.home}/repository/logs" prefix="localhost_access_log_test." suffix=".log" pattern="%{Content-Type}i %{Accept}i %{Accept-Encoding}i" resolveHosts="false"/>
This sample configuration logs the Content-type, Accept and Accept-encoding headers of every request coming to the server. For example, in the following example, we use the RequestInfoExample
to send the HTTP request:
GET http://<IP>:<PORT>/example/servlets/servlet/RequestInfoExample?abc=xyz
It records the following log entry in the localhost_access_log_sample.{DATE}.log
file.
text/plain; charset=utf-8 */* gzip,deflate,sdch
Example 2: Logging response headers
The configuration is as follows:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="${carbon.home}/repository/logs" prefix="localhost_access_log_test." suffix=".log" pattern="%{Content-Type}o %{Content-Length}o %{Date}o %{Server}o" resolveHosts="false"/>
The above configuration sample logs the Content-type
, Content-Length
, Date
and Server
headers of every response coming from the server as follows:
text/html;charset=ISO-8859-1 662 Tue, 09 Jul 2013 11:21:50 GMT WSO2 Carbon
Example 3: Logging other variable values
The configuration is as follows:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="${carbon.home}/repository/logs" prefix="localhost_access_log_test." suffix=".log" pattern="%r %q %h" resolveHosts="false"/>
The above sample configuration logs the first line of the request (method and request URI), query string (prepended with a '?' if it exists) and remote hostname (or IP) of every request coming to the server as follows.
“GET /example/servlets/servlet/RequestInfoExample?abc=xyz HTTP/1.1” ?abc=xyz 10.100.0.67
Example 4: Logging URL encoded parameters
You cannot use the AccessLogValve
to log URL encoded parameters. However, you can use the ExtendedAccessLogValve
attribute for this purpose. In this example only two values (namely, className
and pattern
) are modified from the previous configuration.
The configuration is as follows:
<Valve className="org.apache.catalina.valves.ExtendedAccessLogValve" directory="${carbon.home}/repository/logs" prefix="localhost_access_log_extended." suffix=".log" pattern="x-P(param1) x-P(param2)" resolveHosts="false"/>
Send the POST request together with the URL encoded values such as param1
=value1
and param2
=value2
as follows:
POST http://<IP>:<PORT>/example/servlets/servlet/RequestInfoExample
The above sample configuration logs the following:
'value1' 'value2'