要自定义Apache日志记录,您需要编辑Apache配置文件(通常是httpd.conf或apache2.conf),并使用CustomLog指令来定义日志格式和输出位置
打开Apache配置文件。通常位于以下路径之一:
如果您使用的是虚拟主机,您还需要编辑虚拟主机配置文件,通常位于/etc/httpd/conf.d/或/etc/apache2/sites-available/目录下。
在配置文件中找到CustomLog指令。如果没有找到,请在适当的位置添加它。
CustomLog指令的语法如下:
CustomLog file-path format [env=[!]condition]
其中:
定义日志格式。在配置文件中添加LogFormat指令,语法如下:
LogFormat format-name "%h %l %u %t \"%r\" %>s %b"
其中:
您可以使用其他可用的日志格式占位符,例如%{Referer}i(引用页面)、%{User-Agent}i(用户代理)等。
使用自定义日志格式。将CustomLog指令中的format参数替换为您在LogFormat指令中定义的格式名称。
例如,要将日志记录到文件/var/log/apache2/custom.log,并使用自定义格式“combined”,您可以添加以下指令:
LogFormat "%h %l %u %t \"%r\" %>s %b %{Referer}i %{User-Agent}i" combined
CustomLog /var/log/apache2/custom.log combined
sudo systemctl restart apache2 (Debian/Ubuntu)
sudo systemctl restart httpd (CentOS/RHEL)
现在,Apache将使用您定义的自定义日志格式记录访问日志。