一、場景介紹:
環境:LNMP
我們通常是通過nginx的錯誤日志來分析分錯的,也就是我們在各個server中定義的error_log。
比如下面這樣,就是將錯誤日志定義在/etc/nginx/logs/error/www.xiaobudiu.top.log,發生錯誤,可以查看的對應錯誤日志文件即可。
server { listen 80 default_server; server_name www.xiaobudiu.top; charset utf-8; error_log /etc/nginx/logs/error/www.xiaobudiu.top.log error; access_log /etc/nginx/logs/access/www.xiaobudiu.top.log main; root /data/www; index index.html index.htm index.php; location /favicon.ico { log_not_found off; access_log off; }error_page 404 403 500 502 503 504 /404.html; location = /404.html { root /data/errorPage; }location ~ \.php$ { fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }location ~ /\.ht { deny all; }}
但是,這只是將一個網站的所有錯誤日志都放在了一起,十分不利于分析。
比如,用戶訂單操作錯誤時,我們最好把有關訂單的錯誤寫到一個日志。再比如登錄操作錯誤的,我們最好也是把出錯誤日志寫在登錄錯誤日志中,如何實現呢。
這里,我們只需要在程序中自定義一個錯誤日志函數即可,然后,在程序中進行相應的判斷,如果程序沒執行成功,則調用記錄錯誤日志函數,比如下面這樣。
二、自定義錯誤日志格式,并進行記錄日志
1、程序中編寫相應程序
<?php function set_debug($uid = '', $order = '', $data = ''){$error_path = 'order.error.html';//自定義錯誤日志保存的文件和路徑 $error_data = array('time' => date("Y-m-d H:i",time()),//記錄錯誤發生的時間 'error' => urlencode($data),//防止中文亂碼 'order'=> $order,//記錄訂單 'user_name'=> $uid,//記錄當前用戶 ); //判斷文件大小,選擇追加還是重新寫入,注意之前防止亂碼用了urlencode if( abs(filesize($error_path)) < 10240 ){@file_put_contents($error_path, urldecode(json_encode($error_data))."<br>",FILE_APPEND); }else{@file_put_contents($error_path, urldecode(json_encode($error_data)) ."<br>"); }; }//模擬訂單錄入錯誤時,將日志記錄到錯誤日志中 $uid = 1000070;//模擬用戶uid $order = 2132215641000070;//模擬用戶訂單號 if (true) {set_debug($uid,$order,'訂單錄入失敗'); }
2、創建錯誤文件,并賦予權限
?cd /data/www
?touch order.error.html
?chmod 777 order.error.html?
3、效果
注:鑒于安全考慮,錯誤日志外人肯定是不能訪問到的,所以可以在server中定義正則匹配location,要求指定ip地址段才可以訪問這個文件。可以使用nginx自帶的?http_access_module 模塊進行ip鑒權。當然,也可以采取其他辦法。
這里,以http_access_module 模塊ip鑒權為例。
server { listen 80 default_server; server_name www.xiaobudiu.top; charset utf-8; error_log /etc/nginx/logs/error/www.xiaobudiu.top.log error; access_log /etc/nginx/logs/access/www.xiaobudiu.top.log main; root /data/www; index index.html index.htm index.php; location /favicon.ico { log_not_found off; access_log off; }error_page 404 403 500 502 503 504 /404.html; location = /404.html { root /data/errorPage; }location ~ \.php$ { fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }location ~ ^/order.error.html$ { allow 183.128.227.94; #這個ip能訪問order.error.html deny all; #其他的不可以訪問這個頁面 }location ~ /\.ht { deny all; }}
這樣,就實現了除了我規定的ip地址外,其他ip地址是訪問不到這個文件的。
注:也可以參考https://blog.csdn.net/m_nanle_xiaobudiu/article/details/80688740?最后面內容。
三、使用PHP自帶的error_log 或者?trigger_error 函數
代碼:
<?php $uid = 1000060; if (true) {//采取下面記錄php錯誤日志的其中一種 if (true) {error_log($uid."訂單錄入錯誤,請核實后重新錄入"); }if (true) {trigger_error($uid."訂單錄入錯誤,請核實后重新錄入,heihei~"); } }
效果:
總結:其實在項目中,最好還是使用自定義的記錄錯誤函數比較好,簡單明了,而使用error_log 或者?trigger_error 則沒有太大必要,當做一個輔助措施即可。