Recipe 3.21 Logging User Directories


You want each user directory web site (i.e., those accessed via http://servername/~username) to have its own logfile.


In httpd.conf, add the directive:

CustomLog "|/usr/local/apache/bin/userdir_log" combined

Then, in the file /usr/local/apache/bin/userdir_log, place the following code:

#!/usr/bin/perl my $L = '/usr/local/apache/logs'; # Log directory my %is_open = (  ); # File handle cache $|=1; open(F, ">>$L/access_log"); # Default error log while (my $log = <STDIN>) {     if ($log =~ m!\s/~(.*?)/!) {         my $u = $1;         unless ($is_open{$u}) {             my $fh;             open $fh, '>>' . $L . '/'. $u;             $is_open{$u} = $fh;         }         select ($is_open{$u});         $|=1;         print $log;     }     else {         select F;         $|=1;         print F $log;     } } close F; foreach my $h (keys %is_open) {     close $h; }

(Note that this script is only a skeleton; an actual production quality version should include code to check for errors, etc.)


Usually, requests to user directory web sites are logged in the main server log, with no differentiation between one user's site and another. This can make it very hard for a user to locate log messages for his personal web site.

The Solution allows you to break out those requests into one logfile per user, with requests not going to a userdir web site but going to the main logfile. The log handler can, of course, be modified to put all log messages in the main logfile as well as in the individual logfiles.

File handles are cached to decrease the amount of disk activity necessary, rather than opened and closed with each access. This results in a larger number of file handles open at any given time. Sites with a very large number of user web sites may run out of system resources.

Because Perl buffers output by default, we need to explicitly tell our script not to buffer the output, so that log entries make it into the logfile immediately. This is accomplished by setting the autoflush variable, $|, to a true value. This tells Perl not to buffer output to the most recently selected file handle. Without this precaution, output will be buffered, and it will appear that nothing is being written to your log files.

See Also



Apache Cookbook
Apache Cookbook: Solutions and Examples for Apache Administrators
ISBN: 0596529945
EAN: 2147483647
Year: 2006
Pages: 215

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: