PHP错误日志记录功能,一般用于调试BUG

来源:互联网 发布:最强力的胶水知乎 编辑:程序博客网 时间:2024/04/28 17:16
/** * 错误日志记录 * 大于1MB的时候重新写入,否则追加 * $order 订单号 * $data  错误描述 */function set_debug($order="",$data=""){    $debug_filepath = 'debug.html';//定义保存的文件和路径    $debug_data = array(        'time' => date("Y-m-d H:i",time()),//差8个小时        'error' => urlencode($data),//防止中文乱码        'order'=> $order,//记录订单        'user_name'=> $_SESSION[user_name]?$_SESSION[user_name]:null,//记录当前用户    );    //判断文件大小并选择追加还是重新写入,注意防止乱码用了urldecode    if( abs(filesize($debug_filepath))<10240 ){        @file_put_contents($debug_filepath, urldecode(json_encode($debug_data))."<br>",FILE_APPEND);    }else{        @file_put_contents($debug_filepath, urldecode(json_encode($debug_data)) ."<br>");    };}

最终保存的效果

{"time":"2017-01-24 01:00","error":"订单有误 line:74","order":null,"user_name":null}{"time":"2017-01-24 01:00","error":"订单有误 line:74","order":null,"user_name":null}
0 0
原创粉丝点击