PHP项目中,记录错误日志
一、場景介紹:
環(huán)境:LNMP
我們通常是通過nginx的錯(cuò)誤日志來分析分錯(cuò)的,也就是我們在各個(gè)server中定義的error_log。
比如下面這樣,就是將錯(cuò)誤日志定義在/etc/nginx/logs/error/www.xiaobudiu.top.log,發(fā)生錯(cuò)誤,可以查看的對應(yīng)錯(cuò)誤日志文件即可。
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; }}但是,這只是將一個(gè)網(wǎng)站的所有錯(cuò)誤日志都放在了一起,十分不利于分析。
比如,用戶訂單操作錯(cuò)誤時(shí),我們最好把有關(guān)訂單的錯(cuò)誤寫到一個(gè)日志。再比如登錄操作錯(cuò)誤的,我們最好也是把出錯(cuò)誤日志寫在登錄錯(cuò)誤日志中,如何實(shí)現(xiàn)呢。
這里,我們只需要在程序中自定義一個(gè)錯(cuò)誤日志函數(shù)即可,然后,在程序中進(jìn)行相應(yīng)的判斷,如果程序沒執(zhí)行成功,則調(diào)用記錄錯(cuò)誤日志函數(shù),比如下面這樣。
二、自定義錯(cuò)誤日志格式,并進(jìn)行記錄日志
1、程序中編寫相應(yīng)程序
<?php function set_debug($uid = '', $order = '', $data = ''){$error_path = 'order.error.html';//自定義錯(cuò)誤日志保存的文件和路徑 $error_data = array('time' => date("Y-m-d H:i",time()),//記錄錯(cuò)誤發(fā)生的時(shí)間 'error' => urlencode($data),//防止中文亂碼 'order'=> $order,//記錄訂單 'user_name'=> $uid,//記錄當(dāng)前用戶 ); //判斷文件大小,選擇追加還是重新寫入,注意之前防止亂碼用了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>"); }; }//模擬訂單錄入錯(cuò)誤時(shí),將日志記錄到錯(cuò)誤日志中 $uid = 1000070;//模擬用戶uid $order = 2132215641000070;//模擬用戶訂單號(hào) if (true) {set_debug($uid,$order,'訂單錄入失敗'); }2、創(chuàng)建錯(cuò)誤文件,并賦予權(quán)限
?cd /data/www
?touch order.error.html
?chmod 777 order.error.html?
3、效果
注:鑒于安全考慮,錯(cuò)誤日志外人肯定是不能訪問到的,所以可以在server中定義正則匹配location,要求指定ip地址段才可以訪問這個(gè)文件。可以使用nginx自帶的?http_access_module 模塊進(jìn)行ip鑒權(quán)。當(dāng)然,也可以采取其他辦法。
這里,以http_access_module 模塊ip鑒權(quán)為例。
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; #這個(gè)ip能訪問order.error.html deny all; #其他的不可以訪問這個(gè)頁面 }location ~ /\.ht { deny all; }}
這樣,就實(shí)現(xiàn)了除了我規(guī)定的ip地址外,其他ip地址是訪問不到這個(gè)文件的。
注:也可以參考https://blog.csdn.net/m_nanle_xiaobudiu/article/details/80688740?最后面內(nèi)容。
三、使用PHP自帶的error_log 或者?trigger_error 函數(shù)
代碼:
<?php $uid = 1000060; if (true) {//采取下面記錄php錯(cuò)誤日志的其中一種 if (true) {error_log($uid."訂單錄入錯(cuò)誤,請核實(shí)后重新錄入"); }if (true) {trigger_error($uid."訂單錄入錯(cuò)誤,請核實(shí)后重新錄入,heihei~"); } }效果:
總結(jié):其實(shí)在項(xiàng)目中,最好還是使用自定義的記錄錯(cuò)誤函數(shù)比較好,簡單明了,而使用error_log 或者?trigger_error 則沒有太大必要,當(dāng)做一個(gè)輔助措施即可。
總結(jié)
以上是生活随笔為你收集整理的PHP项目中,记录错误日志的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Navicat 连接 RDS数据库
- 下一篇: php 错误日志 redis' alre