前言 对于开发者而言,日志的分析与排查往往比编写代码本身更具挑战性。本文将梳理常见的PHP日志类型及其具体的配置步骤。
php-fpm 慢日志
PHP的慢查询日志功能依赖于php-fpm的配置。若采用源码包方式安装,默认情况下需执行以下命令进行初始化或修改。
cp php-fpm.conf.default php-fpm.conf
通过源码编译安装的PHP,其配置文件通常位于
/usr/local/php
目录下。若通过yum或其他包管理器安装,不确定具体安装路径时,可执行
find / -name php-fpm.conf
or
php -i | grep Path
------------------------------------------
[root@xxxx etc]# php -i | grep Path
Configuration File (php.ini) Path => /usr/local/php/etc
XPath Support => enabled
Path to sendmail => /usr/sbin/sendmail -t -i
[root@xxxx etc]#
来定位配置文件位置。
旧的版本是在php-fpm.conf设置 (实际是我忘记了哪个版本),php7.x版本源码包编译后需要www.conf修改慢查询配置
vim /usr/local/php/etc/php-fpm.d/www.conf
在较旧的版本中,相关配置可能直接写在php-fpm.conf中(具体版本记忆模糊)。但在PHP 7.x及以上版本的源码编译安装中,慢查询配置通常需要在www.conf中进行修改。
; The log file for slow requests
; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
;slowlog = log/$pool.log.slow
; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_slowlog_timeout = 0
尽管配置项名称一致,若未在php-fpm.conf中找到,请检查同级目录php-fpm.d下的配置文件。 slowlog 用于指定慢查询日志文件的生成路径。 request_slowlog_timeout 用于设定判定请求过长的时间阈值(启用此配置即意味着开启慢查询日志功能),单位通常为秒,常规建议设置为3秒。
php-error 错误日志
在生产环境中,直接暴露PHP报错信息是严格禁止的,这可能导致白屏或返回500错误。因此,建立完善的日志收集机制至关重要。
开启错误日志
通常情况下,PHP的错误日志配置均位于php.ini文件中。
/usr/local/php/etc/php.ini
---------------------------
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
log_errors = On
; Log errors to specified file. PHP's default behavior is to leave this value
; empty.
; http://php.net/error-log
; Example:
;error_log = php_errors.log
; Log errors to syslog (Event Log on Windows).
;error_log = syslog
关键配置参数如下: error_log 指定错误日志的输出文件路径。 error_reporting 生产环境下建议开启所有错误报告级别。 display_errors 设置为关闭,防止错误信息在网页上直接显示。 log_errors 设置为开启,确保错误被记录到文件中。
最终的结果是
error_log = /var/log/php_error.log
display_errors = Off
error_reporting = E_ALL
log_errors = On
到此这篇关于php慢查询日志和错误日志使用详解的文章就介绍到这了,更多相关php慢查询日志和错误日志内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!