PHP code example of baagee / wtf-error
1. Go to this page and download the library: Download baagee/wtf-error library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
baagee / wtf-error example snippets
// 使用内置的错误处理展示
\BaAGee\Wtf\WtfError::register(new \BaAGee\Wtf\Handler\WtfHandler([
'php_error_log_dir' => __DIR__ . '/log',//指定PHP错误log目录,为空 不记录
// 是否是开发调试模式,会显示详细错误。生产环境下很不安全,建议为false
'is_debug' => true
]));
/**
* Class MyHandler
*/
class MyHandler extends \BaAGee\Wtf\Base\WtfHandlerAbstract
{
/**
* 自定义错误异常显示 必须实现
* @param Throwable $t
*/
public function throwableHandler(\Throwable $t)
{
header('content-type: application/json; charset=utf-8');
// 输出json
echo json_encode([
'code' => $t->getCode(),
'message' => $t->getMessage(),
'file' => $t->getFile(),
'line' => $t->getLine(),
], JSON_UNESCAPED_UNICODE);
}
// 自定义写入Log 不是必须的,不用自己调用,定义好方法就会自动调用
public function writePhpErrorLog(\Throwable $t)
{
file_put_contents('./log.log', $t->getMessage() . PHP_EOL, FILE_APPEND);
}
}
\BaAGee\Wtf\WtfError::register(new MyHandler([
'php_error_log_dir' => __DIR__ . '/log',
'is_debug' => true,
'product_error_hidden' => [E_WARNING, E_NOTICE, E_STRICT, E_DEPRECATED]
]));