How to include the server name in the filename of an apache log?
I want to configure apache so that the access and error logs generated from apache include are named as follows:
<server-name>_access_<timestamp>
<server-name>_error_<timestamp>
I have the timestamp part figured out u开发者_JAVA百科sing rotatelogs:
CustomLog logs/access_log combined
CustomLog "|bin/rotatelogs -l /var/logs/access_%Y-%m-%d 86400" common
The part that I cannot figulre out is how to include the server name in the filename. I am configuring Apache on a linux box.
Regards, Mohan
It appears that as recent as Apache HTTPD 2.4, no feature provides this capability.
http://httpd.apache.org/docs/current/mod/mod_log_config.html#customlog
If you wanted to something tricky, you could use a pipe similar to how you're accomplishing your timestamp problem and have a script try to determine the VirtualHost. Not sure how you'd accomplish that.
This is my solution, in the http.conf use:
CustomLog "|$/usr/local/apache2/conf/my_log.pl" common
create a file in /usr/local/apache2/conf/my_log.pl with:
#!/usr/bin/perl
use strict;
use warnings;
my $path="/usr/local/apache2/logs";
my $access="_access.log";
my $hostname=`hostname`;
chomp($hostname);
my $filename="${path}/${hostname}${access}";
$|=1; # Use unbuffered output
open (STDOUT, ">> $filename") or die $!;
while(<STDIN>) {
print STDOUT $_;
}
add the execution perm.:
chmod a+x /usr/local/apache2/conf/my_log.pl
Check out the mod_log_config documentation; iIt looks like you want either %v
or %V
:
%...v The canonical ServerName of the server serving the request.
%...V The server name according to the UseCanonicalName setting.
精彩评论