Networking / Beginners

Using Conditional Logging

Apache also supports conditional logging, which logs specified fields only when certain conditions are met. The conditions that can be tested for are the status codes returned by the server. The status codes are

200: OK The request is valid.

302: Found The requested document was found.

304: Not Modified The requested document has not been modified.

400: Bad Request The request is invalid.

401: Unauthorized The client or user is denied access.

403: Forbidden The requested access is not allowed.

404: Not Found The requested document does not exist.

500: Server Error There was an unspecified server error.

503: Out of Resources (Service Unavailable) The server has insufficient resources to honor the request.

501: Not Implemented The requested server feature is not available.

502: Bad Gateway The client specified an invalid gateway.

To make a field conditional, put a status code on the field in the LogFormat entry. For example, suppose you want to log the browser name only if the browser requests a service that is not implemented in your server. Combine the Not Implemented (501) status code with User-agent header in this manner:

%501{User-agent}i

If this value appears in the LogFormat, the name of the browser is only logged when the status code is 501.

You can also use an exclamation point to specify that you want to log a value only when the status code is not a certain value; the exclamation point indicates "not." For example, to log the address of the site that referred the user to your web page if the status code is not one of the good status codes, add the following to a LogFormat:

%!200,302,304{Referer}i

This particular conditional log entry is actually very useful. It tells you when a remote page has a stale link pointing to your website. It also shows that you can use multiple status codes. Use the LogFormat directive to define exactly what information is logged and the conditions under which it is logged.

[Previous] [Contents]