php慢查询日志和错误日志使用详解

这篇文章主要介绍了php慢查询日志和错误日志使用详解,有感兴趣的可以研究下。

前言

作为一名程序员,比码代码还重要那么一点点的东西就是日志的分析和查询。下面列出常见日志及设置方法。

php-fpm 慢日志

php慢日志需要在php-fpm.conf设置,如果使用源码包安装默认请执行下面命令

cp php-fpm.conf.default php-fpm.conf

默认通过源码包编译安装php目录应在

/usr/local/php

目录下,如果你通过yum或者其他方式安装,不清楚或不知道php具体安装目录,可以使用。

find / -name php-fpm.conf

or

  1. php -i | grep Path
  2. ------------------------------------------
  3. [root@xxxx etc]# php -i | grep Path
  4. Configuration File (php.ini) Path => /usr/local/php/etc
  5. XPath Support => enabled
  6. Path to sendmail => /usr/sbin/sendmail -t -i
  7. [root@xxxx etc]#

开启慢查询日志

旧的版本是在php-fpm.conf设置 (实际是我忘记了哪个版本),php7.x版本源码包编译后需要www.conf修改慢查询配置

vim /usr/local/php/etc/php-fpm.d/www.conf

不过配置项都一样的,如果你在php-fpm.conf找不到,就去他的同级目录php-fpm.d下面找下吧。

  1. ; The log file for slow requests
  2. ; Default Value: not set
  3. ; Note: slowlog is mandatory if request_slowlog_timeout is set
  4. ;slowlog = log/$pool.log.slow
  5. ; The timeout for serving a single request after which a PHP backtrace will be
  6. ; dumped to the 'slowlog' file. A value of '0s' means 'off'.
  7. ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
  8. ; Default Value: 0
  9. ;request_slowlog_timeout = 0

slowlog 设置慢查询日志的生成目录

request_slowlog_timeout 设置慢查询的标准时间(打开此配置就相当于开启了慢查询日志),配置以秒为单位,一般设置3s。

php-error 错误日志

在生产环境中是不允许php报错的,就算报错也是白屏或者500,所以在生产环境中的日志收集是非常重要的。

开启错误日志

一般情况下,php错误日志的配置都在php.ini文件中

  1. /usr/local/php/etc/php.ini
  2. ---------------------------
  3. error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
  4. display_errors = Off
  5. log_errors = On
  6. ; Log errors to specified file. PHP's default behavior is to leave this value
  7. ; emptyempty.
  8. ; http://php.net/error-log
  9. ; Example:
  10. ;error_log = php_errors.log
  11. ; Log errors to syslog (Event Log on Windows).
  12. ;error_log = syslog

error_log 错误日志的生成目录

error_reporting 生产环境错误级别应全开

display_errors 在页面上不显示错误

log_errors 开启错误日志

最终的结果是

  1. error_log = /var/log/php_error.log
  2. display_errors = Off
  3. error_reporting = E_ALL
  4. log_errors = On