要自定义 CentOS 上的 PHP 日志格式,您需要修改 PHP-FPM 配置文件(如果使用 PHP-FPM)或 Apache 配置文件(如果使用 Apache 作为 Web 服务器)。以下是针对这两种情况的步骤:
1. 如果使用 PHP-FPM:
a. 打开 PHP-FPM 配置文件。通常位于 /etc/php-fpm.d/www.conf 或 /etc/php-fpm.d/php-fpm.conf。使用文本编辑器打开,例如:
sudo vi /etc/php-fpm.d/www.conf b. 在配置文件中找到 access.format 设置。如果没有找到,请在文件末尾添加以下行:
access.format = "[%time%] %request% %status% %bytes_sent% %request_uri% %protocol%" c. 自定义日志格式。将 %time%, %request%, %status%, %bytes_sent%, %request_uri%, 和 %protocol% 替换为您想要的日志格式。例如:
access.format = "[%time%] %method% %uri% %status% %size% %referer% %user_agent% %http_referer% %http_user_agent%" d. 保存更改并退出文本编辑器。
e. 重启 PHP-FPM 服务以应用更改:
sudo systemctl restart php-fpm 2. 如果使用 Apache:
a. 打开 Apache 配置文件。通常位于 /etc/httpd/conf/httpd.conf 或 /etc/apache2/apache2.conf。使用文本编辑器打开,例如:
sudo vi /etc/httpd/conf/httpd.conf b. 在配置文件中找到 LogFormat 指令。如果没有找到,请在文件末尾添加以下行:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" custom_log_format c. 自定义日志格式。将 %h, %l, %u, %t, %r, %>s, %b, %{Referer}i, 和 %{User-Agent}i 替换为您想要的日志格式。例如:
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" custom_log_format d. 保存更改并退出文本编辑器。
e. 在 Apache 配置文件中找到 CustomLog 指令。如果没有找到,请在文件末尾添加以下行:
CustomLog /var/log/httpd/access_log custom_log_format 将 /var/log/httpd/access_log 替换为您想要的日志文件路径。
f. 保存更改并退出文本编辑器。
g. 重启 Apache 服务以应用更改:
sudo systemctl restart httpd 现在,您的 PHP 日志应该已经使用自定义格式记录。请注意,根据您的系统和 PHP 版本,配置文件的位置和名称可能会有所不同。