29.10. Configuring Logging

By default, every request that Apache finishes processing is written to a log file in a standard format. For each request, the client IP address, website username, date, time, URL path, status code, and number of bytes transferred is logged. In the default Apache configuration, there is only a single log file that is used for all virtual servers. You can, however, reconfigure the web server to use different files for different virtual hosts, and even to log additional information for each request.

Apache also has a log file for recording error messages, which are generated when a browser requests a page that does not exist, when an HTTP connection is terminated, or if some other unexpected condition occurs. As Section 29.8 “Running CGI Programs” explains, this log file also contains error output from CGI programs and failure messages from server-side include directives.

To see which log files are being used by Apache on your system and to change them, follow these steps:

1.
On the Apache Webserver module's main page, click on the Default Server icon. This will bring you to the default server options page similar to the one shown in Figure 29.4.

2.
Click on the Log Files icon to bring up the log files configuration form shown in Figure 29.7.

Figure 29.7. Default server log files configuration form.


3.
The Error log to field controls where CGI and web server error messages are written. Typically, the File option is selected and the path to a file into which error messages should be written is displayed in the field next to it.

You can select the System Log option if you want to have messages sent to syslog instead (covered in Chapter 13). All messages will use the local7 facility.

The other available option is Program, which, when selected, will cause Apache to run the command entered into the field next to it and feed error log messages to it as input. This can be useful for performing your own filtering or analysis of errors as they are reported.

4.
The Named log format table lists predefined formats that can be used for logfiles defined in the next step. Each has a Nickname which is used to refer to it, and a Format string that specifies the fields written to the log for each request. When a log line is written, each of the % fields in the format string is replaced by some detail of the request, such as the client address, HTTP status code, or virtual server name. See the online Apache documentation for more details on which % fields are available.

Several standard formats such as common and combined are already defined in the default Apache configuration. To create your own log format, fill in the empty row at the bottom of the table. Each format must have a unique nickname.

5.
The Access log files table specifies the files that are used for logging actual requests processed by the Apache Web server. Multiple files can be specified and the format of each can be selected independently from one of those explained in the previous step. All requests will be written to all listed logfiles.

Each row of the table defines one logfile. Under the Format column you can choose the format for the file or select the Default option to use the standard Apache log file format. Under the Write to column, you can choose whether the logging is being done to a file or to the input of a program. The path to that file or program must be entered into the field in the File or program column.

If you want to add an additional log file, fill in the fields in the empty row at the bottom of the table.

6.
If you have made any changes to the logging configuration, click the Save button at the bottom of the page and then the Apply Changes link.

Apache also allows you to define different log files for each virtual server, so requests to the various virtual hosts on your system do not all get mixed up into one file. By default, all requests are written to a single access log file without any field that identifies the virtual server that processed them. To change this and have a virtual server write to its own separate log file, use the following process:

1.
On the module's main page, click on the icon of the virtual server for which you want to configure a new log file.

2.
Click on the Log Files icon, which will take you to a page similar to the one in Figure 29.7.

3.
If you want this virtual server to have its own separate error log file, change the Error log to field from Default to one of the other options.

4.
To add a log format that exists only for this virtual server, fill in the empty row in the Named log formats table. It is usually a better idea to define all log formats in the default server, however, so they can be used in any virtual host.

5.
Add a row to the Access log files table for this virtual server's separate log file. As soon as one is defined, requests to the virtual host will be written to it only instead of the access log list on the Log Files page under the default server.

6.
When done, click the Save button at the bottom of the page to have your new log-file settings written to the Apache configuration. Then, back on the virtual server options page, hit the Apply Changes link at the top to make them active.

If you have multiple virtual servers and want to identify to which one each request was made, another solution is to change the format of the default access log file to include the virtual server hostname in each log line. To set this up, follow these steps:

1.
On the module's main page, click on the Default Server icon and then on the Log Files icon on the default server options page.

2.
Find the row for the common format in the Named log formats table and change its Format field so that it reads

%h %l %u %t "%r" %>s %b %{Host}i "%{Referer}i" "%{User-Agent}i"

The extra fields will tell Apache to include the virtual server hostname, referrer URL, and browser name for each request on every log line.

3.
Find the row for your server's main log file in the Access log files table and make sure that the Format field is set to common, rather than Default or some other named format.

4.
Click the Save button, and then the Apply Changes link. All entries written to the logfile from now on will include the additional information.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.70.170