【问题标题】:How to include the server name in the filename of an apache log?如何在 apache 日志的文件名中包含服务器名称?
【发布时间】:2011-03-06 10:35:30
【问题描述】:

我想配置 apache,让 apache include 生成的访问和错误日​​志命名如下:

<server-name>_access_<timestamp>
<server-name>_error_<timestamp>

我已经使用 rotatelogs 计算出时间戳部分:

CustomLog logs/access_log combined
CustomLog "|bin/rotatelogs -l /var/logs/access_%Y-%m-%d 86400" common

我无法弄清楚的部分是如何在文件名中包含服务器名称。我在 linux 机器上配置 Apache。

问候, 磨难

【问题讨论】:

    标签: linux apache filenames logging


    【解决方案1】:

    似乎在最近的 Apache HTTPD 2.4 中,没有任何功能提供此功能。

    http://httpd.apache.org/docs/current/mod/mod_log_config.html#customlog

    如果您想要一些棘手的事情,您可以使用类似于完成时间戳问题的管道,并让脚本尝试确定 VirtualHost。不知道你会如何做到这一点。

    【讨论】:

      【解决方案2】:

      这是我的解决方案,在http.conf中使用:

      CustomLog "|$/usr/local/apache2/conf/my_log.pl" common
      

      在 /usr/local/apache2/conf/my_log.pl 中创建一个文件:

      #!/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 $_;
      }
      

      添加执行权限:

      chmod a+x /usr/local/apache2/conf/my_log.pl
      

      【讨论】:

        【解决方案3】:

        查看the mod_log_config documentation; i看起来你想要%v%V

        %...v    The canonical ServerName of the server serving the request.
        %...V    The server name according to the UseCanonicalName setting.
        

        【讨论】:

        • 这些设置是指各个日志消息的格式,而不是日志文件本身的名称。
        猜你喜欢
        • 2018-09-03
        • 2020-02-03
        • 2019-04-11
        • 2014-11-17
        • 2013-07-25
        • 2020-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多