Hi,
I've just written a short perl script to perform logging for our virtual
hosts. The code has plenty of comments so I'll paste it below.
My question is: would it be possible to use mod_perl in some way to
perform the function of the script? In testing the speed of the script
seems reasonable enough, is there a better way to do what I'm doing
below?
On a related note, I'd ideally like to be able to do similar for Apache
Error logging - ie log apache error log entries once into a main
errorlog file and once into a vhost errorlog file. Is this possible
with ErrorLog lines?
My initial understanding is this isn't possible using the framework
outlined in the code below.
Code follows:
#!/usr/bin/perl
# Script to pipe apache log entries to virtually hosted log files
# Assumes httpd.conf has the following:
# LogFormat "%v %h %l %u %t \"%r\" %>s %b" commonvhost
# and that ONLY the following logging line is used in the httpd.conf:
# CustomLog "| /path/to/logger.pl" commonvhost
# DO NOT configure the CustomLog directive in the vhost stubs for vhosts
# or this will not work.
# Script logs commonvhost entries to a logfile with a template of:
# /var/log/httpd/virtual.domain/$year/$month/$day
use strict;
my $logEntry = <>;
# get the vhost from this log entry:
$logEntry=~/(.*?) /;
my $vhost = $1;
my ($year, $month, $day) = (
(localtime)[5]+1900,
sprintf("%02d", (localtime)[4]+1),
sprintf("%02d", (localtime)[3])
);
# Name of access logfiles:
my $accessLogName = "httpd-access.log";
=comment
$logdir:
Location to put all logfiles
This will log everything into:
$logdir/all/$year/$month/$day/httpd-access.log
and put individual vhost logfiles into:
$logdir/$vhost/$year/$month/$day/httpd-access.log
=cut
my $logDir = "/var/log/httpd";
my $allLogDir = "$logDir/all/$year/$month/$day";
my $vhostLogDir = "$logDir/$vhost/$year/$month/$day";
writeLog($allLogDir, "all");
writeLog($vhostLogDir, "vhost");
# write a log entry to a file
sub writeLog(){
my $logDir = shift @_;
my $type = shift @_;
if( ! -d $logDir ) {
`mkdir -p $logDir`;
}
open(FD, ">>$logDir/$accessLogName");
# if type is vhost, strip off the vhost data:
if($type eq "vhost"){
$logEntry =~s/.*? //;
}
print FD $logEntry;
close FD;
}
--
Jez
http://www.munk.nu/