Apache:是否可以ErrorLoglogging主机名而不是IP地址?

在几乎只有DHCP客户端的低负载int ra网上有不同的Apache服务器,我们需要logging主机名而不是IP地址。 由于DHCP环境非常dynamic,以后任何将IP重新映射到主机名的尝试都很可能会产生错误的结果。

虽然我们有“ HostnameLookups On ”,但只有访问日志乖乖logging主机名,但ErrorLog不会。

阅读ErrorLogFormat ,我注意到没有%h ,只是%a (意思是“客户端IP地址和端口”)。

那么真的没有办法让Apache也logging错误日志中的主机名…?

与ErrorLog指令不是原生的。

我会做的是写一个脚本,为您解决并通过pipe道ErrorLog。 在你的Apacheconfiguration是这样的:

 Errorlog "|/usr/local/bin/errorlog_resolver.pl" 

然后是一个Perl脚本示例:

 #!/usr/bin/perl -w # errorlog_resolver.pl # Give apache ErrorLog on STDIN, outputs them with numeric IP addresses # in the likely (host) field converted to hostnames (where possible). # based on clf_lookup.plx from "Perl for Web Site Management" # http://oreilly.com/catalog/perlwsmng/chapter/ch08.html # use strict; use Socket; open LOGFILE, ">>/tmp/my_error_log" or die "Couldn't open file: $!"; my %hostname; while (<>) { my $line = $_; my($day, $month, $dayn, $hour, $year, $err, $client, $host, $rest) = split / /, $line, 9; if ( $client ~~ "[client" ) { # remove the ] trailing the likely ip-address. my $chr = chop($host); if ($host =~ /^\d+\.\d+\.\d+\.\d+$/) { # looks vaguely like an IP address unless (exists $hostname{$host}) { # no key, so haven't processed this IP before $hostname{$host} = gethostbyaddr(inet_aton($host), AF_INET); } if ($hostname{$host}) { # only processes IPs with successful lookups $line = "$day $month $dayn $hour $year $err $client $hostname{$host}\($host\)\] $rest)"; } } } print LOGFILE $line; }