nadar/yii2-sentry
Composer 安装命令:
composer require nadar/yii2-sentry
包简介
Yii2 Sentry integration with log target support
README 文档
README
Yii2 Sentry integration with log target support for error tracking and monitoring.
Important
Yii might look inactive, but it is still used a lot. This Sentry extension is actively supported, works with current PHP versions, and supports the latest Sentry SDK options.
Requirements
- PHP >= 8.4
- Yii2 >= 2.0
- Sentry PHP SDK >= 4.9
Installation
Install via Composer:
composer require nadar/yii2-sentry
Configuration
Basic Configuration
Configure the Sentry component in your application config:
return [ 'components' => [ 'sentry' => [ 'class' => 'Nadar\Sentry\Sentry', 'dsn' => 'YOUR_SENTRY_DSN', ], ], ];
Console Test Command (Optional)
For console applications, you can add the Sentry test command to verify your integration:
return [ 'controllerMap' => [ 'sentry-test' => [ 'class' => 'Nadar\Sentry\SentryTestCommand', ], ], ];
Then test your Sentry integration by running:
php yii sentry-test
This will send various test events to Sentry including exceptions, messages with different severity levels, log events, and events with custom context data. Check your Sentry dashboard to verify the events appear correctly.
Log Target Configuration
Add the Sentry log target to your log component:
return [ 'components' => [ 'log' => [ 'targets' => [ [ 'class' => 'Nadar\Sentry\SentryTarget', 'levels' => ['error', 'warning'], 'except' => [ 'yii\web\HttpException:404', 'yii\web\HttpException:403', ], 'logVars' => ['_GET', '_POST', '_SESSION', '_SERVER'], ], ], ], ], ];
Verifying Installation
After installing and configuring the package, you can verify that Sentry is working correctly:
- Configure the Sentry component in your
config/console.php(for console apps) orconfig/web.php(for web apps) - Add the test command to your console config (see Console Test Command section below)
- Run the test command:
php yii sentry-test
- Check your Sentry dashboard at https://sentry.io/ to see the test events
The test command will send multiple test events including exceptions, messages, and events with custom context to help you verify your integration is working correctly.
Configuration Options
Sentry Component Options
- dsn (required): Your Sentry DSN (Data Source Name)
- sampleRate: Sample rate for error events (0.0 to 1.0, default: 1.0)
- tracesSampleRate: Sample rate for performance monitoring (0.0 to 1.0, default: 0.0)
- sendDefaultPii: Whether to send default PII (Personally Identifiable Information)
- maxBreadcrumbs: Maximum breadcrumbs (default: 100)
- clientOptions: Additional client options for Sentry SDK
- extraCallback: Global callback function to add extra data to all events (see below)
Log Target Options
- sentry: The Sentry component ID (default: 'sentry')
- levels: Array of log levels to capture (e.g., ['error', 'warning'])
- except: Array of patterns to exclude from logging (e.g., ['yii\web\HttpException:404'])
- logVars: Array of context variables to log (e.g., ['_GET', '_POST', '_SERVER'])
Usage
Manual Exception Capture
You can manually capture exceptions or messages:
try { // Your code } catch (\Exception $e) { Yii::$app->sentry->captureException($e); }
Manual Message Capture
Yii::$app->sentry->captureMessage('Something went wrong', 'error');
Using Yii2 Logger
The log target will automatically capture messages logged through Yii2's logger:
Yii::error('An error occurred'); Yii::warning('A warning message');
Extra Callbacks
Both the Sentry component and SentryTarget support extraCallback functions to add custom context data to events. When both callbacks are defined, they are merged together with the SentryTarget callback taking precedence.
Define a global extraCallback in the Sentry component to add context data to ALL events:
return [ 'components' => [ 'sentry' => [ 'class' => 'Nadar\Sentry\Sentry', 'dsn' => 'YOUR_SENTRY_DSN', 'extraCallback' => function () { return [ 'server_id' => gethostname(), 'app_version' => '1.0.0', ]; }, ], ], ];
nadar/yii2-sentry 适用场景与选型建议
nadar/yii2-sentry 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.35k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「logging」 「sentry」 「error tracking」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nadar/yii2-sentry 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nadar/yii2-sentry 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nadar/yii2-sentry 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Fork of a PHP client for Sentry (http://getsentry.com) that work also with php5.2
An implementation of the Sentry User Manager for Laravel.
Simple Yii2 Sentry target for logging (http://getsentry.com)
A Zend Framework module that sets up Monolog for logging in applications.
Monitoring for scheduled jobs
Asynchronous Sentry for Symfony - Fire and forget
统计信息
- 总下载量: 1.35k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04