文章來自:www.startphp.cn/front/php/1206179.h...
作者:磊豐
系統崩潰了,或者網站響應很慢,網站出現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
------------------------------------------
[[email protected] 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
[[email protected] 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.
; 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
本作品採用《CC 協議》,轉載必須註明作者和本文連結