laykou / rollbar-php
监控错误和异常并向Rollbar报告。支持代理身份验证。
该包的官方仓库似乎已消失,因此该包已被冻结。
This package is not auto-updated.
Last update: 2024-01-20 13:20:40 UTC
README
Rollbar的PHP通知器。捕获并向Rollbar.com报告异常,用于警报、报告和分析。
代理身份验证
与原始Rollbar PHP插件相比,此插件也支持代理身份验证。您可以这样配置
// With authentication $config = [ 'proxy' => [ 'address' => '123.123.123.123:9876', 'username' => 'my_name', 'password' => 'my_password' ] ]; // Or you can use only the address: $config = [ 'proxy' => '123.123.123.123:9876' ]; Rollbar::init($config);
快速入门
<?php // installs global error and exception handlers Rollbar::init(array('access_token' => 'POST_SERVER_ITEM_ACCESS_TOKEN')); try { throw new Exception('test exception'); } catch (Exception $e) { Rollbar::report_exception($e); } // Message at level 'info' Rollbar::report_message('testing 123', 'info'); // With extra data (3rd arg) and custom payload options (4th arg) Rollbar::report_message('testing 123', 'info', // key-value additional data array("some_key" => "some value"), // payload options (overrides defaults) - see api docs array("fingerprint" => "custom-fingerprint-here")); // raises an E_NOTICE which will *not* be reported by the error handler $foo = $bar; // will be reported by the exception handler throw new Exception('test 2'); ?>
安装
通用
下载 rollbar.php 并将其放置在可访问的位置。
如果使用Composer
将 Laykou/rollbar-php
添加到您的 composer.json
{ "require": { "Laykou/rollbar-php": "*" } }
设置
在应用程序的入口点添加以下代码
<?php require_once 'rollbar.php'; $config = array( // required 'access_token' => 'POST_SERVER_ITEM_ACCESS_TOKEN', // optional - environment name. any string will do. 'environment' => 'production', // optional - path to directory your code is in. used for linking stack traces. 'root' => '/Users/brian/www/myapp' ); Rollbar::init($config); ?>
请确保将 POST_SERVER_ITEM_ACCESS_TOKEN
替换为您的项目 post_server_item
访问令牌,您可以在Rollbar.com界面上找到它。
这将安装一个异常处理程序(使用 set_exception_handler
)和一个错误处理程序(使用 set_error_handler
)。如果您不想这样做
<?php $set_exception_handler = false; $set_error_handler = false; Rollbar::init($config, $set_exception_handler, $set_error_handler); ?>
Heroku用户
首先,添加插件
heroku addons:add rollbar:free
access_token
和 root
配置变量将自动检测,因此配置很简单
<?php Rollbar::init(array( 'environment' => 'production' )); ?>
基本用法
就这样!未捕获的错误和异常现在将报告给Rollbar。
如果您想报告您自己捕获的异常
<?php try { do_something(); } catch (Exception $e) { Rollbar::report_exception($e); } ?>
您还可以发送类似Rollbar日志的消息
<?php Rollbar::report_message('could not connect to mysql server', 'warning'); Rollbar::report_message('Here is a message with some additional data', 'info', array('x' => 10, 'code' => 'blue')); ?>
批量处理
默认情况下,有效负载将批量发送,在脚本执行结束时通过关闭处理程序发送到Rollbar服务器,或者当批量大小达到50时,以先到者为准。这对于标准短生命周期的脚本,如处理Web请求,非常有效。
如果您在一个长时间运行的脚本中使用Rollbar,例如Laravel项目或后台工作进程,您可能希望手动刷新批量。要刷新,只需调用
Rollbar::flush();
例如,如果使用Laravel,将上述行添加到您的 App::after()
事件处理程序。或者在循环后台工作进程的每次循环结束时调用它。
您还可以调整最大批量大小或完全禁用批量处理。请参阅下面的 batch_size
和 batched
配置变量。
配置
异步报告
默认情况下,有效负载(批量或非批量)作为脚本执行的一部分发送。这很容易配置,但可能会对性能产生负面影响。通过一些额外的设置,可以将有效负载写入本地中继文件;该文件将被rollbar-agent异步消费。要启用此功能,设置以下配置参数:
<?php $config = array( // ... rest of current config 'handler' => 'agent', 'agent_log_location' => '/var/www' // not including final slash. must be writeable by the user php runs as. ); ?>
您还需要运行代理。有关设置说明,请参阅rollbar-agent文档。
配置参考
以下所有选项都可以作为$config
数组中的键传递。
- access_token
- 您的项目访问令牌。
- agent_log_location
- 代理中继日志文件应写入的目录的路径。不应包含最后的反斜杠。仅在处理器为`agent`时使用。
默认值:
/var/www
- base_api_url
- 要提交的基准API URL。
默认值:
https://api.rollbar.com/api/1/
- batch_size
- 如果达到此大小,则提前刷新批次。
默认值:
50
- batched
- 为真时,将单个请求中的所有报告一起批量处理。
默认值:
true
- branch
- 当前分支的名称。
默认值:
master
- capture_error_stacktraces
- 记录PHP错误的完整堆栈跟踪。
默认值:
true
- code_version
- 您的代码/应用程序当前部署的版本(例如Git SHA)。应为字符串。
默认值:
null
- environment
- 环境名称,例如`'production'`或`'development'`。
- error_sample_rates
- 关联数组,将错误编号映射到采样率。采样率是1的比例,例如0是“从不报告”,1是“始终报告”,0.1是“10%的时间报告”。采样是基于每个错误进行的。
默认值:空数组,表示报告所有错误。
- handler
- 可以是`'blocking'`或`'agent'`。`'blocking'`使用curl立即发送请求;`'agent'`将中继日志写入要由[rollbar-agent](https://github.com/rollbar/rollbar-agent)消费的。
默认值:
'blocking'
- host
- 服务器主机名。
默认值:
null
,这将导致调用gethostname()
(如果该函数不存在,则调用php_uname('n')
)。 - included_errno
- 一个掩码,包括要报告的所有错误级别。例如,`(E_ERROR | E_WARNING)`仅报告E_ERROR和E_WARNING错误。这将与`error_reporting()`结合使用,以防止在`use_error_reporting`设置为`true`时报告错误。
默认值:(E_ERROR | E_WARNING | E_PARSE | E_CORE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR)
- logger
- 具有`log($level, $message)`方法的对象。如果提供,将由RollbarNotifier用于记录消息。
- person
- 包含有关当前登录用户数据的关联数组。必需:`id`,可选:`username`,`email`。所有值都是字符串。
- person_fn
- 函数引用(字符串等 - [call_user_func()](https://php.ac.cn/call_user_func)可以处理的内容)。返回一个类似于'person'的数组。
- root
- 您的项目根目录的路径。
- scrub_fields
- 要从_POST和_SESSION中清除的字段名称数组。值将被替换为星号。如果覆盖,请确保列出您想要清除的所有字段,而不仅仅是您想要添加到默认值的字段。参数名称在比较之前转换为小写。
默认值:
('passwd', 'password', 'secret', 'confirm_password', 'password_confirmation', 'auth_token', 'csrf_token')
- shift_function
- 是否在堆栈跟踪中将函数名称下移一个帧,以便函数名正确反映每个帧的上下文。
默认值:
true
- 超时
- 将请求发送到Rollbar的超时时间(秒)。
默认:
3
- report_suppressed
- 设置是否应该报告使用'@'抑制的错误
默认:
false
- use_error_reporting
- 设置是否尊重当前的`error_reporting()`级别
默认:
false
error_sample_rates的使用示例
<?php $config['error_sample_rates'] = array( // E_WARNING omitted, so defaults to 1 E_NOTICE => 0.1, E_USER_ERROR => 0.5, // E_USER_WARNING will take the same value, 0.5 E_USER_NOTICE => 0.1, // E_STRICT and beyond will all be 0.1 ); ?>
person_fn的使用示例
<?php function get_current_user() { if ($_SESSION['user_id']) { return array( 'id' => $_SESSION['user_id'], // required - value is a string 'username' => $_SESSION['username'], // optional - value is a string 'email' => $_SESSION['user_email'] // optional - value is a string ); } return null; } $config['person_fn'] = 'get_current_user'; ?>
相关项目
对于Laravel的集成,有一个特定的Laravel包可用:[Laravel-Rollbar](https://github.com/jenssegers/Laravel-Rollbar)
帮助/支持
如果您遇到任何问题,请通过电子邮件发送至support@rollbar.com
您也可以在IRC中找到我们:chat.freenode.net上的#rollbar频道
对于错误报告,请在GitHub上创建一个问题。
贡献
- 分叉它
- 创建您的功能分支(
git checkout -b my-new-feature
) - 提交您的更改(
git commit -am 'Added some feature'
) - 推送到分支(
git push origin my-new-feature
) - 创建新的Pull Request