要自定義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將使用您定義的自定義日志格式記錄訪問日志。