Error-Handler 是用于捕获PHP项目运行期间的各类异常错误,支持通过monolog或者sentry进行错误上报.
与Sentry官方的加载方式相比,该包只在捕获到异常时才进行实例化并报告异常,所以消耗的服务器资源更少,这也是这个包诞生的原因。
composer require gaowei-space/error-handler
$options = [
'report_level' => E_ALL,
'display_errors' => true,
'handler' => 'sentry', // sentry or logger
'sentry_options' => [
'dsn' => 'http://[email protected]/3',
'environment' => 'test',
'sample_rate' => 1,
'http_timeout' => 0.5,
],
'scope_user' => [
'id' => 10,
'username' => '丹',
],
'scope_tags' => [
'game_role' => '菜鸟',
],
];
ErrorHandler::init($options);
$logger = new Logger("errors");
$logger->pushHandler(new StreamHandler(sprintf('%s/log/errors_%s.log', __DIR__, date('Ymd')), Logger::DEBUG, true, 0666));
$options = [
'report_level' => E_ALL,
'display_errors' => true,
'handler' => 'logger', // sentry or logger
'logger' => $logger,
];
ErrorHandler::init($options);
composer require gaowei-space/error-handler --dev
cp examples/.env.example examples/.env
SENTRY_DSN = "http://[email protected]/3"
// monolog
php examples/Monolog.php
// sentry
php examples/Sentry.php
$options = [
'report_level' => E_ALL, // error report level
'display_errors' => true, // prite errors
'handler' => 'sentry', // sentry or logger
'sentry_options' => [
'dsn' => 'http://[email protected]/3', // sentry website dsn
'environment' => 'test',
'sample_rate' => 1, // report rate, float range 0-1
'http_timeout' => 0.5,
],
];
本扩展包:
ErrorHandler::init($options); // 耗时: 0.001616
Sentry 官方:
\Sentry\init($options['sentry_options']); // 耗时: 0.146600
本项目采用 MIT 开源授权许可证,完整的授权说明已放置在 LICENSE 文件中。