Home > Backend Development > PHP Problem > php reports errors and records them by itself

php reports errors and records them by itself

Release: 2023-02-28 09:54:01
Original
2461 people have browsed it

php reports errors and records them by itself

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 = &#39;&#39;, $order = &#39;&#39;, $data = &#39;&#39;){
    $error_path = &#39;order.error.html&#39;;//自定义错误日志保存的文件和路径
    $error_data = array(
        &#39;time&#39; => date("Y-m-d H:i",time()),//记录错误发生的时间
        &#39;error&#39; => urlencode($data),//防止中文乱码
        &#39;order&#39;=> $order,//记录订单
        &#39;user_name&#39;=> $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,&#39;订单录入失败&#39;);
}
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template