没有不值得去解决的问题,也没有不值得去学习的技术!

Integrating Sentry 9 With the Yii 2 Advanced Project Template

In the All options for configuring your application, the Yii2 framework is not available, so you can only select PHP

1. In the All options for configuring your application, the Yii2 framework is not available, so you can only select PHP, as shown in Figure 1.

In the All options for configuring your application, the Yii2 framework is not available, so you can only select PHP
Figure 1

2. Install the PHP integration for Sentry. The recommended method is to use Composer, but I decided not to use this approach for now, as shown in Figure 2.

 

3. Searching for yii2 sentry on GitHub, I decided to implement this based on hellowearemito/yii2-sentry, as shown in Figure 3.

Install the PHP integration for Sentry. The recommended method is to use Composer, but I decided not to use this approach for now
Figure 2

3. Searching for yii2 sentry on GitHub, I decided to implement this based on hellowearemito/yii2-sentry, as shown in Figure 3.

Searching for yii2 sentry on GitHub, I decided to implement this based on hellowearemito/yii2-sentry
Figure 3

4. Open the URL: https://github.com/hellowearemito/yii2-sentry and execute the command, as shown in Figure 4.

Open the URL: https://github.com/hellowearemito/yii2-sentry and execute the command
Figure 4
Bash

composer require --prefer-dist mito/yii2-sentry "~1.0.0"

5. After installing the extension, set the configuration in the development environment configuration file by editing \environments\dev\api\config\main-local.php.

PHP

return [
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => '',
        ],
        'sentry' => [
            'class' => 'mito\sentry\Component',
            'enabled' => false, // 设置为 false 以跳过收集错误,即禁用 Sentry,默认:true
            'dsn' => 'YOUR-PRIVATE-DSN', // 私有 DSN
            'environment' => 'development', // 环境,development:开发环境;production:生产环境,默认:production
            'jsNotifier' => false, // 收集 JS 错误,默认:false
            'jsOptions' => [ // raven-js 配置参数
                'whitelistUrls' => [ // 收集JS错误的网址
                    'http://api.gitlab-php-yii2-app-advanced-cmc.localhost',
                ],
            ],
        ],
    ],
];

6. After installing the extension, set the configuration in the production environment configuration file by editing \environments\prod\api\config\main-local.php.

PHP

return [
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => '',
        ],
        'sentry' => [
            'class' => 'mito\sentry\Component',
            'enabled' => false, // 设置为 false 以跳过收集错误,即禁用 Sentry,默认:true
            'dsn' => 'YOUR-PRIVATE-DSN', // 私有 DSN
            'environment' => 'production', // 环境,development:开发环境;production:生产环境,默认:production
            'jsNotifier' => false, // 收集 JS 错误,默认:false
            'jsOptions' => [ // raven-js 配置参数
                'whitelistUrls' => [ // 收集JS错误的网址
                    'http://api.gitlab-php-yii2-app-advanced-cmc.localhost',
                ],
            ],
        ],
    ],
];

7. Configure the log component to support sending logs to Sentry by editing \api\config\main.php.

PHP

    'components' => [
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                'sentry' => [
                    'class' => 'mito\sentry\Target',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
    ],

8. Initialize by selecting the development environment and edit \api\config\main-local.php, as shown in Figure 5.

Initialize by selecting the development environment and edit \api\config\main-local.php
Figure 5
Plaintext

init
0
yes
All

9. The private DSN value needs to be copied from the Configuration section at /settings/sentry/pcs-api/install/php/, as shown in Figure 6.

The private DSN value needs to be copied from the Configuration section at /settings/sentry/pcs-api/install/php/
Figure 6

10. Make a GET request to the API, which returns a 302 response, as shown in Figure 7.

Make a GET request to the API, which returns a 302 response
Figure 7
JSON

{
    "name": "Found",
    "message": "框架服务控制台HTTP请求失败:登录超时",
    "code": 20039,
    "status": 302,
    "type": "yii\\web\\HttpException"
}

11. Check the Unresolved Issues, which are the unresolved problems. As expected, the corresponding log has been sent to Sentry, as shown in Figure 8.

Check the Unresolved Issues, which are the unresolved problems. As expected, the corresponding log has been sent to Sentry
Figure 8

12. Initialize by selecting the production environment and edit \api\config\main-local.php, as shown in Figure 9.

Initialize by selecting the production environment and edit \api\config\main-local.php
Figure 9
Plaintext

init
1
yes
All

13. Make a GET request to the API, which returns a 404 response, as shown in Figure 10.

Make a GET request to the API, which returns a 404 response
Figure 10
JSON

{
    "name": "Not Found",
    "message": "用户ID:10,不存在",
    "code": 20002,
    "status": 404
}

14. Check the Unresolved Issues, which are the unresolved problems. As expected, the corresponding log has been sent to Sentry, as shown in Figure 11.

Check the Unresolved Issues, which are the unresolved problems. As expected, the corresponding log has been sent to Sentry
Figure 11

15. After initializing by selecting the development environment, do not edit \api\config\main-local.php, as shown in Figure 12.

After initializing by selecting the development environment, do not edit \api\config\main-local.php
Figure 12

16. Make a GET request to the API, which returns a 404 response, as shown in Figure 13.

Make a GET request to the API, which returns a 404 response
Figure 13
JSON

{
    "name": "Not Found",
    "message": "用户ID:11,不存在",
    "code": 20002,
    "status": 404,
    "type": "yii\\web\\NotFoundHttpException"
}

17. Check the Unresolved Issues, which are the unresolved problems. As expected, the corresponding log was not sent to Sentry because Sentry has been disabled, as shown in Figure 14.

Check the Unresolved Issues, which are the unresolved problems. As expected, the corresponding log was not sent to Sentry because Sentry has been disabled
Figure 14

18. Repeat steps 15, 16, and 17 again, selecting the production environment in step 15. I found that the corresponding log was not sent to Sentry, as expected, because Sentry has been disabled, as shown in Figure 15.

Repeat steps 15, 16, and 17 again, selecting the production environment in step 15. I found that the corresponding log was not sent to Sentry, as expected, because Sentry has been disabled
Figure 15

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系