The system crashed and the website response was slow. How do you quickly locate the error information?

Posted by StewardManscat on Tue, 07 Dec 2021 16:36:40 +0100

Article from: www.startphp.cn/front/php/1206179.h...
Author: Lei Feng


The system crashed, or the website response was very slow, and the website appeared 502... These problems often occur at work or in the development process. These questions may be often asked during the interview. So how do you check for errors and locate error messages at the first time!
If the above errors occur, we often think of logs. Yes, as a programmer, a little more important than code is log analysis and query. Let's take a look at some common logs and setting methods: the log settings of Nginx and PHP FPM


access.log and error.log of Nginx

1. There are two commonly used configuration files for nginx: access.log and error.log
access.log is used to record all user access requests, regardless of the status code, including 200404500 and other requests. 404500 requests will not appear in error.log.
The function of error.log is to record some errors in the running of nginx itself, and will not record user access requests. For example, the format of recording module error information log and nginx configuration file error log does not support customization, and the level can be set.

2. Set access_log
Access logs are mainly used to record client requests. Every request from the client to the nginx server will be recorded in access_log. It includes the request IP, time, access url, etc. of course, we can record the specific log information in the access log through log_format settings
View log storage address

find / -name nginx.conf

According to the queried address, enter the nginx.conf file to find access_log and error_ Path to the log file
access_log setting syntax:

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

#The allowed variables in the log format are as follows:
$remote_addr, $http_x_forwarded_for  //Record client IP address
$remote_user   //Record client user name
$request      //Record the URL and HTTP protocol of the request
$status    //Record request status
$body_bytes_sent    //The number of bytes sent to the client, excluding the size of the response header; This variable is the same as the Apache module mod_ log_ The '% B' parameter in config is not compatible.
$bytes_sent   //The total number of bytes sent to the client.
$connection   //Serial number of the connection.
$connection_requests   //The number of requests currently obtained through a connection.
$msec        //Log write time. The unit is seconds and the precision is milliseconds.
$pipe       //If the request is sent through HTTP pipelined, the pipe value is "p", otherwise it is ".".
$http_referer      //Record which page link you accessed from
$http_user_agent   //Record client browser related information
$request_length   //The length of the request (including the request line, request header and request body).
$request_time    //Request processing time, in seconds, with precision of milliseconds; Start from reading the first byte of the client until the log is written after sending the last character to the client.
$time_iso8601    //Local time in ISO8601 standard format.
$time_local      //Local time in common log format.

Reference example

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. Set error_log
Configure the path and level of the error log file

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

The first parameter refers to the path to write the error log
The second parameter refers to the level of the log. The level can be any value in debug, info, notice, warn, error, crit, alert and emerg. Only when the error level of the log is greater than or equal to the value specified by level will it be written to the error log. The default value is error

error.log configuration example:

#Error log save location
#error_log logs/error.log;

#Specify the location and level of the error log
#error_log logs/error.log notice;
#error_log logs/error.log info;


What are the two kinds of logs you need to know in PHP development

There is such a scenario
In the test environment, I did not encounter any problems in many tests, but in one online process, the online environment experienced a response timeout of 20 seconds, which is obviously a problem of the environment. Although there is a large amount of online data, MySQL is not as slow as 20 seconds, and it is found that every time it is between 20.01 and 20.04 seconds, with a difference of less than one second, and MySQL is not so uniform, At this time, you may check the MySQL slow query log. If you find an sql log without timeout, it may be a problem in php. At this time, you need to check the php slow log.

PHP FPM slow log
PHP slow log needs to be set in php-fpm.conf. If you use the source package to install the default, please execute the following command

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

By default, php is compiled and installed through the source package. The directory should be

/usr/local/php

Directory, if you install through yum or other methods and don't know or know the specific php installation directory, you can use

find / -name php-fpm.conf

perhaps

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]#


Enable slow query log
The old version of PHP is set here in php-fpm.conf, while the source package of php7.x needs to modify the slow query configuration at www.conf after compilation

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

The configuration items are the same. If you can't find php-fpm.conf, go to its peer directory 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 sets the generation directory of slow query logs
  • request_slowlog_timeout sets the standard time of slow query (opening this configuration is equivalent to opening the slow query log). The configuration is in seconds, generally 3s.


PHP error error log
php is not allowed to report errors in the production environment. Even if an error is reported, it is also a white screen or 500. Therefore, log collection in the production environment is very important.

Open error log
Generally, the configuration of PHP error log is in php.ini file

/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 log generation directory
error_reporting The production environment error level should be fully open
display_errors Do not display errors on the page
log_errors Open error log

The end result is

error_log = /var/log/php_error.log
display_errors = Off
error_reporting = E_ALL
log_errors = On

Topics: Laravel