We usually analyze errors through the nginx error log, which is the error_log we define in each server. Let's take a look at how PHP records errors by itself.
php records errors by itself:
We only need to customize an error log function in the program, and then, in The corresponding judgment is made in the program. If the program is not executed successfully, the error log function is called.
Customize the error log format and record the log:
1. Write the corresponding program in the program
<?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. Create the error file and grant permissions
cd /data/www
touch order.error.html
chmod 777 order.error.html
Recommended:phpserver
The above is the detailed content of php reports errors and records them by itself. For more information, please follow other related articles on the PHP Chinese website!