您的位置 首页 php

系统奔溃了,网站响应慢了,你是如何快速定位错误信息的?

系统奔溃了,或者网站响应很慢,网站出现502。。。这些问题在工作上,或者开发过程中经常出现吧。这些问题或许在面试的时候也会经常被问到。那么你是怎么在第一时间检查错误,定位错误信息的呢!


出现以上的错误的话,我们经常想到的是日志吧。是的,作为一名程序员,比码代码还重要那么一点点的东西就是日志的分析和查询。下面来看看一些常见日志及设置方法:
nginx 的日志设置与 php-fpm 的一些日志设置

Nginx access.log error.log
1 nginx 常用的配置文件有两种: access.log error.log
access.log 的作用是:记录用户所有的访问请求,不论状态码,包括200 ,404,500等请求,404,500的请求并不会出现在 error.log 中。

error.log 的作用是:记录 nginx 本身运行时的一些错误,不会记录用户访问的请求。比如记录模块错误信息日志,以及nginx配置文件的错误日志等,格式不支持自定义,可以设置级别。

2 设置 access_log
访问日志主要用于记录客户端的请求。客户端向 nginx 服务器发起的每一次请求都会被记录到 access_log 中。包含请求 IP 、时间、访问 url 等等,当然访问日志中具体记录哪些日志信息我们可以通过 log_format 设置.

查看日志存放地址

 find / -name nginx.conf  


根据查询出来地址,进入
nginx.conf 文件查找 access_log error_log 文件的路径
access_log 的设置语法:

 log_format  combined  '$remote_addr - $remote_user  [$time_local]'
                      ' "$ request "  $status  $body_bytes_sent'
                      ' "$http_referer"  "$http_user_agent"';


#日志格式允许包含的变量注释如下:
$remote_addr, $http_x_forwarded_for  //记录客户端IP地址
$remote_user   //记录客户端用户名称
$request      //记录请求的URL和HTTP协议
$status    //记录请求状态
$body_bytes_sent    //发送给客户端的字节数,不包括响应头的大小; 该变量与Apache模块mod_log_config里的“%B”参数兼容。
$ bytes _sent   //发送给客户端的总字节数。
$connection   //连接的序列号。
$connection_requests   //当前通过一个连接获得的请求数量。
$msec        //日志写入时间。单位为秒,精度是毫秒。
$pipe       //如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”。
$http_referer      //记录从哪个页面链接访问过来的
$http_user_agent   //记录客户端浏览器相关信息
$request_length   //请求的长度(包括请求行,请求头和请求正文)。
$request_time    //请求处理时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。
$time_ iso8601     //ISO8601标准格式下的本地时间。
$time_local      //通用日志格式下的本地时间。  

参考实例

 http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                                        '"$status" $body_bytes_sent "$http_referer" '
                                        '"$http_user_agent" "$http_x_forwarded_for" '
                                        '"$gzip_ratio" $request_time $bytes_sent $request_length';

    log_format srcache_log '$remote_addr - $remote_user [$time_local] "$request" '
                                '"$status" $body_bytes_sent $request_time $bytes_sent $request_length '
                                '[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]';

    open_log_ File _cache max=1000 inactive=60s;

    server {
        server_name ~^(www.)?(.+)$;
        access_log logs/$2-access.log main;
        error_log logs/$2-error.log;

        location /srcache {
            access_log logs/access-srcache.log srcache_log;
        }
    }
}  


3 设置 error_log
配置错误日志文件的路径和级别

 error_log file [level];
Default:   
error_log logs/error.log error;  

第一个参数指写入错误日志的路径
第二个参数指日志的级别。
level 可以是: debug info notice warn error crit alert emerg 中的任意值。只有日志的错误级别大于等于 level 指定的值才会被写入错误日志中,默认值是 error .

error.log 配置示例:

 #错误日志保存位置
#error_log logs/error.log;

#指定错误日志的位置和级别
#error_log logs/error.log notice;
#error_log logs/error.log info;  


PHP开发中需要了解的两种日志是什么

有这么一种情景
测试环境 多番测试没有遇到问题,但在一次上线过程中,在线上环境出现20秒的响应超时,这个显然是环境的问题了,尽管在线上数据量大,可Mysql也不至于慢到20秒,而且发现每次都是20.01~20.04秒之间,相差不到一秒钟,mysql也不至于这么均匀,这个时候你可能会去查看Mysql慢查询日志,如果发现没有超时的sql日志,那这有可能就是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  

或者

 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旧的版本是在php-fpm.conf这里设置的,而
php7.x 版本源码包编译后需要 www.conf 修改慢查询配置

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

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

 ; 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  
  • slowlog 设置慢查询日志的生成目录
  • request_slowlog_timeout 设置慢查询的标准时间(打开此配置就相当于开启了慢查询日志),配置以秒为单位,一般设置3s。


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.
; 
; 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技术社区

感谢阅读!

文章来源:智云一二三科技

文章标题:系统奔溃了,网站响应慢了,你是如何快速定位错误信息的?

文章地址:https://www.zhihuclub.com/152236.shtml

关于作者: 智云科技

热门文章

网站地图