Apache/tomcat access log

Apache(web server/HTTP server) and tomcat basically sharing similar configuration for access log.

Turning ON and OFF the log:

Apache HTTP server (configuration file of httpd.conf normally located in “conf” folder): the logging is defined by a CustomLog directive for access logs and an ErrorLog directive for error logs. For example,

CustomLog /usr/local/apache/logs/access_log common
ErrorLog “logs/error_log”

Note: CustomLog and ErrorLog are also been defined within VirtualHost declaration too for logging the activity of particular virtual host. Disabling logging for virtual hosts only may cause the log information been written to main Apache log files.

For tomcat, it is defined by valve. Tomcat valve – a new technology introduced with Tomcat 4 which allows you to associate an instance of a Java class with a particular Catalina container. This configuration allows the named class to act as a preprocessor of each request. These classes are called valves, and they must implement the org.apache.catalina.Valve interface or extend the org.apache.catalina.valves.ValveBase class. Valves may not be used in a different servlet/JSP container.

The access log valve is the first of the Tomcat prepackaged valves is the Access Log valve: org.apache.catalina.valves.AccessLogValve. It creates log files to track client access information.Some of the content that it tracks includes page hit counts, user session activity, user authentication information, and much more. The Access Log valve can be associated with an engine, host, or context container. It could be configured with configuration file (tomcat_home/conf/Catalina/localhost/<contextpath>.xml).

<Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs” prefix=”localhost_access_log.” suffix=”.txt” pattern=”common”/>

Configuring what to log

For Apache, details well

http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#formats

For Tomcat <Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs” prefix=”localhost_access_log.” suffix=”.txt” pattern=”% XXXXXXXXX” resolveHosts=”false”/>

%a – Remote IP address
%A – Local IP address
%b – Bytes sent, excluding HTTP headers, or ‘-‘ if zero
%B – Bytes sent, excluding HTTP headers
%h – Remote host name (or IP address if resolveHosts is false)
%H – Request protocol
%l – Remote logical username from identd (always returns ‘-‘)
%m – Request method (GET, POST, etc.)
%p – Local port on which this request was received
%q – Query string (prepended with a ‘?’ if it exists)
%r – First line of the request (method and request URI)
%s – HTTP status code of the response
%S – User session ID
%t – Date and time, in Common Log Format
%u – Remote user that was authenticated (if any), else ‘-‘
%U – Requested URL path
%v – Local server name
%D – Time taken to process the request, in millis
%T – Time taken to process the request, in seconds
%I – current request thread name (can compare later with stacktraces)

I have long requests need to show…

Sometimes when the request is long, the default log configuration could only show part of the link, not the full request. You need to write a filter and apply this filter to your configuration, land then utilize it in your application.

Sample detail steps available here for tomcat: http://www.coderanch.com/t/484631/Tomcat/configure-Tomcat-log-POST-data

1) Add the following class under WEB-INF/classes/filter/

package filters;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.*;

public final class PostDataDumperFilter implements Filter {

private FilterConfig filterConfig = null;

public void destroy() {
this.filterConfig = null;
}

public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOExceptionServletException {
if (filterConfig == null)
return;

Enumeration names = request.getParameterNames();
StringBuffer output=new StringBuffer();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
output.append(name+”=”);
String values[] = request.getParameterValues(name);
for (int i = 0; i < values.length; i++) {
if (i > 0) output.append(“‘ “);
output.append(values[i]);
}
if(names.hasMoreElements()) output.append(“&”);
}
request.setAttribute(“postdata”, output);
chain.doFilter(request, response);
}

public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
}

2) add the filter conf in WEB-INF/web.xml:
<filter>
<filter-name>post-data-dumper-filter</filter-name>
<filter-class>filters.PostDataDumperFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>post-data-dumper-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

3) now you have the attribute postdata for each request and you can easily log it in access valve, for example:
<Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs” prefix=”access.” suffix=”.log” pattern=’%h %p %H %l %u %t “%r” [%{postdata}r] %s %b %T’ resolveHosts=”false”/>

6 comments

  1. Thank you for keeping us updated. I very enjoy it and find all the information very useful.

  2. Bid-Ninja…

    […]we like to honor other sites on the web, even if they aren’t related to us, by linking to them. Below are some sites worth checking out[…]…

  3. Aw, this was a incredibly high quality post. In theory I’d like to write like this as well – taking time and genuine effort to generate a beneficial article… but what can I say… I procrastinate alot and by no means look to obtain something done.

  4. Whats up, This really is a excellent summation, I located your blog checking google for any similar subject and observed this. I couldnt discover as well a lot other tips and info on this posting, so it was wonderful to find this one. I will possibly be returning to contemplate many other content which you have written an additional time.

  5. Hi! I could have sworn I’ve visited your blog before but after browsing through many of the posts I realized it’s new to me.
    Anyhow, I’m certainly pleased I stumbled upon it and I’ll be bookmarking it and checking back often!

  6. I can see you are an expert at your field! I am launching a web site soon, and your data will likely be very interesting for me.. Thanks for all your help and wishing you all the success.

Leave a comment