Advanced Logging Techniques With Apache Page 3

Subdividing the Logs

Logging in Apache
Creating Custom Log Formats
Conditional Logging
Subdividing the Logs
Logging Directly to a Database or Application
Speeding up the Logging Process

Conditional logging enables you to configure information written to a log, but it's limited to controlling the output of a particular CustomLog definition. Sometimes it's preferable to divide up the content of the log file based on the available information, such as host, referrer, or HTTP headers.

The typical log configuration creates a separate access and error log for each server and virtual host the Apache installation is serving. To configure logs for virtual hosts, place the ErrorLog and CustomLog directives within the VirtualHost directive:

<VirtualHost *>
        ServerAlias www.mcslp.pri www
        DocumentRoot /export/http/webs/pri.mcslp
        ServerName www.mcslp.pri
        ErrorLog logs/www-error_log
        CustomLog logs/www-access_log common
        ScriptAlias /cgi-bin/ /export/http/webs/pr.mcslp/cgi-bin/

The logs can even be divided up further to create additional information, for example the error output from scripts and CGI applications, as well as referrer information. For Web programmers in particular, the the first option is invaluable, as it enables an admin to monitor the errors in a Web application without having to sift through the general error or access log to find needed information.

A number of options are available for dividing up logs without using the conditional logging system:

  • The CookieLog directive configures Apache to log both incoming and outgoing cookies sent from the browser and returned by applications on the Web server. This is very useful when testing but generally useless in a production environment.
  • The ScriptLog directive configures Apache to place the error output from CGI applications into a separate log. Again, this is not recommended on production servers. You can further configure the log with a maximum bugger size and a maximum log length. For more information, consult the mod_cgi documentation.
  • The RewriteLog directive configures Apache to record results of rewrite rules. Although you can use this on a production server, it's likely that once the rewrite system is configured, you won't need the output.
  • A separate log of referrers (the URLs pointing to a site), User Agents (browsers), and other HTTP request header information can be configured through the CustomLog directive by specifying a format using the %{}i format string. For example, %{Referrer}i would log the referrer info.

All of these directives apply globally or within each VirtualHost directive.

One final trick with the custom log formatting system is to selectively log fields based on the status code of the request. Normally, all requests are logged, even if the request failed for some reason. By prefixing a list of status codes before each field, status-based logs are created. If, for example, you wanted to log all redirection requests to a specific log to determine whether an old URL was still being used, you could parse the log to determine this, or you could create a special log containing just this information using the following fragment in the configuration file:

LogFormat "%301U" redirects
CustomLog logs/redirects.log redirects

Now, it is evident at a glance which URLs are still being used and redirected to new locations.

>> Logging Directly to a Database or Application

This article was originally published on Jul 2, 2004
Page 3 of 4

Thanks for your registration, follow us on our social networks to keep up-to-date