Customizing Logs

Using the LogFormat and CustomLog directives, you can customize your log record to add more fields with varying levels of detail. These directives use a format string consisting of field specifiers to determine the fields to record in a log record. You add whatever fields you want and in any order. A field specifier consists of a percent (%) symbol followed by an identifying character. For example, %h is the field specifier for a remote host, %b for the size in bytes, and %s for the status. See the documentation for the mod_log_config module for a complete listing. You should quote fields whose contents may take up more than one word. The quotes themselves must be quoted with a backslash to be included in the format string. The following example is the Common Log Format implemented as a FormatLog directive:

FormatLog "%h %l %u %t \"%r\" %s %b"

Certain field specifiers in the log format can be qualified to record specific information. The %i specifier records header lines in requests the server receives. The reference for the specific header line to record is placed within braces between the % and the field specifier. For example, User-agent is the header line that indicates the browser software used in the request. To record User-agent header information, use the conversion specifier %{User-agent}i.

To maintain compatibility with NCSA servers, Apache originally implemented AgentLog and RefererLog directives to record User-agent and Referer headers. These have since been replaced by qualified %i field specifiers used for the LogFormat and CustomLog directives. A Referer header records link information from clients, detecting who may have links to your site. The following is an NCSA-compliant log format:

"%h %l %u %t \"%r\" %s %b\"%{Referer}i\" \"%{User-agent}i\"". Generating and Managing Log Files

Instead of maintaining one large log file, you can create several log files using the CustomLog or TransferLog directive. This is helpful for virtual hosts where you may want to maintain a separate log file for each host. You use the FormatLog directive to define a default format for log records. The TransferLog then uses this default as its format when creating a new log file. CustomLog combines both operations, enabling you to create a new file and to define a format for it.

FormatLog "%h %l %u %t \"%r\" %s %b"

# Create a new log file called myprojlog using the FormatLog format TransferLog myprojlog

# Create a new log file called mypicslog using its own format CustomLog mypicslog "%h %l %u %t \"%r\" %s %b"

Apache provides two utilities for processing and managing log files: logresolve resolves IP addresses in your log file to hostnames; rotatelogs rotates log files without having to kill the server. You can specify the rotation time.

NOTE The Apache Web server can also provide detailed reports on server activity and configuration, letting you display this information to remote servers. The Location directive server-info will display the configuration details of your Web server, and the server-status directive will show Web processes. The pages server-info and server-status will display the reports, as in http:// localhost/server-info. Use the ExtendedStatus directive to enable detailed reports.

Was this article helpful?

0 0

Post a comment