gaowei-space / error-handler
ErrorHandler is used to catch all php runtime errors and supports reporting to monolog or sentry.
1.2.0
2023-04-12 03:13 UTC
Requires
- php: ^7.2|^8.0
- monolog/monolog: ^2.6
- psr/log: ^1.1
- sentry/sdk: ^3.2
Requires (Dev)
- vlucas/phpdotenv: ^5.4
README
Error-Handler 是用于捕获PHP项目运行期间的各类异常错误,支持通过monolog或者sentry进行错误上报.
与Sentry官方的加载方式相比,该包只在捕获到异常时才进行实例化并报告异常,所以消耗的服务器资源更少,这也是这个包诞生的原因。
安装
composer require gaowei-space/error-handler
使用
1. sentry
$options = [ 'report_level' => E_ALL, 'display_errors' => true, 'handler' => 'sentry', // sentry or logger 'sentry_options' => [ 'dsn' => 'http://0c2f5aaca4a14eaf958a050157843090@sentry.yoursentrysite.com/3', 'environment' => 'test', 'sample_rate' => 1, 'http_timeout' => 0.5, ], 'scope_user' => [ 'id' => 10, 'username' => '丹', ], 'scope_tags' => [ 'game_role' => '菜鸟', ], ]; ErrorHandler::init($options);
2. monolog
$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);
测试
1. 安装开发扩展包
composer require gaowei-space/error-handler --dev
2. 复制配置文件
cp examples/.env.example examples/.env
3. 编辑配置文件
SENTRY_DSN = "http://0c2f5aaca4a14eaf958a050157843090@sentry.yoursentrysite.com/3"
4. 运行测试代码
// monolog php examples/Monolog.php // sentry php examples/Sentry.php
与Sentry官方实例化进行对比如下
$options = [
'report_level' => E_ALL, // error report level
'display_errors' => true, // prite errors
'handler' => 'sentry', // sentry or logger
'sentry_options' => [
'dsn' => 'http://0c2f5aaca4a14eaf958a050157843090@sentry.yoursentrysite.com/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 文件中。