PHP快速排查定位问题

方法一、服务器日志

ps aux | grep nginx  //执行命令看主进程master process,有nginx的安装位置和配置文件的位置

/usr/sbin/nginx -t    //获取当前配置nginx.config 配置文件位置

tail -f /var/log/nginx/t3.local-error.log  //查看错误日志

 

方法二、

phpinfo();  找到配置文件,找到错误日志,开启报错级别,查看错误日志;

找到display_errors = On;

error_reporting=E_ALL &  ~E_NOTICE;

 

方法三、

跟踪路由找到入口文件,浏览器打印调试问题;

数据库开启打印查看写入SQL情况

#查看日志情况
show variables like '%general%';
#开启日志
SET GLOBAL general_log = 'On';
#指定日志文件
SET GLOBAL general_log_file = '/var/lib/mysql/mysql.log';

 

答案分析:set_error_handler() 可指定一个回调函数,错误发生时,会自动通过指定的回调函数处理。在回调函数中抛出新的异常即可。 

 

方法四、

####
#### 通过回溯函数定位文件错误位置
#### register_shutdown_function
####
<?php

/**
* 公共返回封装
* Class CommonReturn
*/
class CommonReturn
{

/**
* 打包函数
* @param $params
* @param int $status
*
* @return mixed
*/
static public function packData($params, $status = 0)
{
ob_start();
//打印了一条 PHP 回溯
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 5);
//得到当前缓冲区的内容并删除当前输出缓
$trace = ob_get_clean();
register_shutdown_function(array('CommonReturn','handleFatal'), $trace);
//register_shutdown_function(function(){
//var_dump(error_get_last());
//});
        $res['status'] = $status;
$res['data'] = json_encode($params);
return $res;
}

/**
* 错误处理
* @param $trace
*/
static public function handleFatal($trace)
{
//获取最近发生的错语
$err = error_get_last();
if ($err['type']) {
$log_cont = 'time=%s' . PHP_EOL . 'error_get_last:%s' . PHP_EOL . 'trace:%s' . PHP_EOL;
@file_put_contents('/tmp/debug_' . __FUNCTION__ . '.log', sprintf($log_cont, date('Y-m-d H:i:s'), var_export($err, 1), $trace), FILE_APPEND);
}

}
}
$CommonReturn = new CommonReturn;
$data = $CommonReturn::packData([1],1);
var_dump($data);
exit;

方法五、函数执行全过程
<?php

class a{
function say($msg) {
echo "msg:".$msg;
echo "<pre>";debug_print_backtrace();
}
}

class b {
function say($msg) {
$a = new a();
$a->say($msg);
}
}

class c {
function __construct($msg) {
$b = new b();
$b->say($msg);
}
}

$c = new c("test");

msg:test
#0  a->say(test) called at [E:\phpstudy_pro\WWW\project\index.php:13]
#1  b->say(test) called at [E:\phpstudy_pro\WWW\project\index.php:20]
#2  c->__construct(test) called at [E:\phpstudy_pro\WWW\project\index.php:24]

方法五、
通过xdebug 工具调试 或 kint-php调试工具
https://www.jianshu.com/p/77d21d8cfbb7
posted @ 2020-07-24 10:30  快乐的在一起  阅读(560)  评论(0编辑  收藏  举报