coamw2 / phpbrake
PHP的Airbrake异常和错误通知器
Requires
- php: >=8.1
- cash/lrucache: ^1.0
- guzzlehttp/guzzle: >=6.3
Requires (Dev)
- mikey179/vfsstream: ^1.6
- monolog/monolog: ^3.4.0
- phpunit/phpunit: ^10.3.1
- squizlabs/php_codesniffer: ^2.9
Suggests
- guzzlehttp/guzzle: Guzzle HTTP client
This package is not auto-updated.
Last update: 2024-09-24 17:47:44 UTC
README
PHPBrake
特性
PHPBrake是官方的Airbrake PHP错误通知器。PHPBrake支持PHP 5.4及以上版本。PHPBrake包含许多有用的功能,让您能够控制向Airbrake发送什么以及何时发送,您可以使用
- 从您的代码中的try-catch块发送通知
- 向通知添加自定义数据
- 从通知中过滤敏感数据
- 忽略特定的异常
- 配置错误处理器以捕获未捕获的异常
- 与monolog集成
- 与Laravel集成
- 与CakePHP 3.x集成
- 与Symfony集成
- 与Zend集成
- 等等
安装
composer require airbrake/phpbrake
快速入门
// Create new Notifier instance. $notifier = new Airbrake\Notifier([ 'projectId' => 12345, // FIX ME 'projectKey' => 'abcdefg' // FIX ME ]); // Set global notifier instance. Airbrake\Instance::set($notifier); // Register error and exception handlers. $handler = new Airbrake\ErrorHandler($notifier); $handler->register(); // Somewhere in the app... try { throw new Exception('hello from phpbrake'); } catch(Exception $e) { Airbrake\Instance::notify($e); }
API
通知API包含4个方法
buildNotice
- 构建Airbrake通知。sendNotice
- 将通知发送到Airbrake。notify
-buildNotice
和sendNotice
的快捷方式。addFilter
- 添加可以修改和/或过滤通知的过滤器。
向通知添加自定义数据
$notifier->addFilter(function ($notice) { $notice['context']['environment'] = 'production'; return $notice; });
从通知中过滤敏感数据
$notifier->addFilter(function ($notice) { if (isset($notice['params']['password'])) { $notice['params']['password'] = 'FILTERED'; } return $notice; });
忽略特定的异常
$notifier->addFilter(function ($notice) { if ($notice['errors'][0]['type'] == 'MyExceptionClass') { // Ignore this exception. return null; } return $notice; });
向通知添加用户数据
$notifier->addFilter(function ($notice) { $notice['context']['user']['name'] = 'Avocado Jones'; $notice['context']['user']['email'] = 'AJones@guacamole.com'; $notice['context']['user']['id'] = 12345; return $notice; });
设置严重性
严重性允许对错误的严重性进行分类。默认设置为error
。要重新定义严重性,只需覆盖通知对象的context/severity
即可。例如
$notice = $notifier->buildNotice($e); $notice['context']['severity'] = 'critical'; $notifier->sendNotice($notice);
错误处理器
通知器可以处理PHP错误、未捕获的异常和关闭。您可以使用以下代码注册适当的处理器
$handler = new Airbrake\ErrorHandler($notifier); $handler->register();
在底层,$handler->register
执行以下操作
set_error_handler([$this, 'onError'], error_reporting()); set_exception_handler([$this, 'onException']); register_shutdown_function([$this, 'onShutdown']);
Laravel集成
见https://github.com/TheoKouzelis/laravel-airbrake
Symfony集成
见https://github.com/aminin/airbrake-bundle
CakePHP 3.x集成
见https://gist.github.com/mauriciovillalobos/01a97f9ee6179ad70b17d54f37cc5010
Zend Framework集成
见https://github.com/FrankHouweling/zend-airbrake
Monolog集成
$log = new Monolog\Logger('billing'); $log->pushHandler(new Airbrake\MonologHandler($notifier)); $log->addError('charge failed', ['client_id' => 123]);
额外配置选项
appVersion
您可以将应用程序的版本传递给此选项,以区分多个版本之间的异常。默认情况下不设置。
$notifier = new Airbrake\Notifier([ // ... 'appVersion' => '1.2.3', // ... ]);
host
默认情况下设置为api.airbrake.io
。一个host
是一个包含方案("http"或"https")、主机和端口的Web地址。您可以省略端口(默认为80)和方案(默认为"https")。
$notifier = new Airbrake\Notifier([ // ... 'host' => 'errbit.example.com', // put your errbit host here // ... ]);
remoteConfig
配置远程配置功能。通知器每10分钟向Airbrake服务器发送一个GET请求,以获取包含项目配置设置的JSON文档。通知器将在运行时应用这些新设置。默认情况下,此功能已启用。
要禁用此功能,请使用以下方式配置您的通知器
$notifier = new Airbrake\Notifier([ // ... 'remoteConfig' => false, // ... ]);
注意:不建议禁用此功能。它可能会对通知器的工作产生负面影响。请谨慎使用此选项。
rootDirectory
配置您的项目根目录。期望输入一个字符串或路径名,表示项目路径。提供此选项有助于我们从堆栈跟踪中过滤重复数据,并从我们的仪表板链接到GitHub文件。
$notifier = new Airbrake\Notifier([ // ... 'rootDirectory' => '/var/www/project', // ... ]);
环境
配置应用程序运行的环境。帮助Airbrake仪表板区分不同环境中发生的异常。默认情况下,未设置。
$notifier = new Airbrake\Notifier([ // ... 'environment' => 'staging', // ... ]);
httpClient
配置必须实现GuzzleHttp\ClientInterface
的基本http客户端。
// Supply your own client. $client = new Airbrake\Http\GuzzleClient( new GuzzleHttp\Client(['timeout' => 3]) ); $notifier = new Airbrake\Notifier([ // ... 'httpClient' => $client, // ... ]);
过滤键
使用keysBlocklist
选项,您可以指定包含敏感信息的键列表,这些键必须被过滤掉,例如。
$notifier = new Airbrake\Notifier([ // ... 'keysBlocklist' => ['/secret/i', '/password/i'], // ... ]);
运行测试
composer install vendor/bin/phpunit
PHPDoc
composer require phpdocumentor/phpdocumentor vendor/bin/phpdoc -d src firefox output/index.html
联系
如果您有任何问题、疑问或错误报告,请随时
许可证
PHPBrake在MIT许可证(MIT)下授权。