Back when I still used apache, I learned a trick from a coworker. He piped the global access log through an awk
script. The awk
script in turn would then take care creating the different logfiles.
In httpd.conf:
LogFormat "%V %p %a %l %u %t \"%r\" %s %b \"%200{Referer}i\" \"%200{User-agent}i\" \"%{cookie}n\"" awklogpipe
CustomLog "|/usr/local/bin/apacheawklogpipe" awklogpipe env=!dontlog
The script /usr/local/bin/apacheawklogpipe:
#!/bin/gawk -f
BEGIN {
months["Jan"] = "01";
months["Feb"] = "02";
months["Mar"] = "03";
months["Apr"] = "04";
months["May"] = "05";
months["Jun"] = "06";
months["Jul"] = "07";
months["Aug"] = "08";
months["Sep"] = "09";
months["Oct"] = "10";
months["Nov"] = "11";
months["Dec"] = "12";
}
{
# HEADS UP: SET THIS!!!!
LOGBASE="/var/log/httpd/access"
SSL=""
# Automagicly set first
SITE=tolower($1);
PORT=$2
if ($2 == 443)
{
SSL="-ssl"
}
# Extract all but first two fields (vhostname vhostport to be exactly) into LINE
LINE=substr($0,index($0,$3));
# No matter where it is, we will find an apache datestamp.
match(LINE,/\[[0-9]+\/[A-Z][a-z]+\/[0-9]+:[0-9]+:[0-9]+:[0-9]+[\t ]+[+-][0-9]+\]/);
split(substr(LINE,RSTART + 1,RLENGTH - 2), ap_log_time, /[\/: ]/);
#ap_rotatelog= LOGBASE "/" SITE ":" PORT "/access-log" SSL "-" ap_log_time[3] "-" months[ap_log_time[2]] ap_log_time[1];
ap_rotatelog= LOGBASE "/" SITE "/access_log" SSL;
if (system("test -d " LOGBASE "/" SITE ) == 0)
{
print LINE >> ap_rotatelog;
close(ap_rotatelog);
}
else
{
print SITE "\t" SSL "\t" LINE >> LOGBASE "/w3logrotate-error.log";
close(LOGBASE "/w3logrotate-error.log");
}
}
Make sure /usr/local/bin/apacheawklogpipe is executable. All you would need to take care of with this script, is create a directory in /var/log/httpd/access
that corresponds to the virtualhostname. I had a script that would create a virtualhost config and create the log directories.