定制 sheershoff/yii2-sentry-component 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sheershoff/yii2-sentry-component

Composer 安装命令:

composer require sheershoff/yii2-sentry-component

包简介

A Yii2 component for Sentry allowing data arrays passing through Yii2 logging methods

README 文档

README

This fork is abandoned and left for those who still uses it (if any at all). Stick to https://github.com/E96/yii2-sentry instead.

Yii2 sentry component

Yii2 Sentry component allowing for unified array format passing parameters to Sentry log target and other log targets. The ability to pass arrays as the log message is suitable practice for those who want to use Logstash, ElasticSearch, etc. for getting statistical inferences of these data later on.

Install

php composer.phar require sheershoff/yii2-sentry-component:dev-master

In config file:

'bootstrap' => ['log', 'raven'],
'components' => [
    'raven' => [
        'class' => 'sheershoff\sentry\ErrorHandler',
        'dsn' => '', // Sentry DSN
    ],
    'log' => [
        'targets' => [
            [
                'class' => 'sheershoff\sentry\Target',
                'levels' => ['error', 'warning'],
                'dsn' => '', // Sentry DSN
            ]
        ],
    ],
]

If application doesn't have raven component, the component will not try to send messages to sentry. This is useful for development environments, for example.

Usage

Exceptions and PHP errors are caught without effort. Standart Yii::(error|warning|info|trace) logging works as usual, but you also can use the following format:

Yii::warning([
    'msg' => 'SomeWarning', // event name that will be sent to Sentry
    'data' => [ // extra data for the event
        'userId' => Yii::$app->user->id,
        'someDataOnWarningSituation' => $controller->state,
        'modelThatCausedFailure' => $model->attributes,
    ],
], 'eventCategory');

Or you can replace this with Log::warning as in the exception example below, since the exception argument is not required. Notice that eventCategory is not sent to Sentry and is used only for log messages routing and filtering.

Wherever you need to log a caught exception with stacktrace and additional data, use

use sheershoff\sentry\Log;
// some code here
try{
    $model1->save();
}catch (\Exception $e){
    Log::warning([
        'msg' => 'ExceptionWarning', // event name that will be sent to Sentry
        'data' => [ // extra data for the event
            'userId' => Yii::$app->user->id,
            'someDataOnWarningSituation' => $controller->state,
            'modelThatCausedFailure' => $model->attributes,
        ],
    ], 'exceptionCategory', $e);
}

There are proxy methods in Log for the four Yii static methods: error, warning, info, trace. If $e is not null the component expects that it is an exception and after calling the corresponding Yii method also captures the exception for Sentry.

Also, the following use cases are possible:

SentryHelper::extraData($task->attributes);
throw new Exception('unknown task type');

Or just capture a message with full stacktrace:

try {
    throw new Exception('FAIL');
} catch (Exception $e) {
    SentryHelper::captureWithMessage('Fail to save model', $e);
}

Other log targets

To use the power of the component you should keep in mind that other log targets will receive arrays instead of strings in the log message. You can create a proxy class, redefining formatMessage method of the parent LogTarget, e.g.:

namespace common\components;
use Yii;
class SyslogJsonTarget extends \yii\log\SyslogTarget
{
	/**
	 * @inheritdoc
	 */
	public function formatMessage($message)
	{
		list($text, $level, $category, $timestamp) = $message;
		$level = \yii\log\Logger::getLevelName($level);
		if (!is_string($text)) {
			$text = \yii\helpers\Json::encode($text);
		} else {
			$text = \yii\helpers\Json::encode(['rawstring' => $text]);
		}
		$prefix = $this->getMessagePrefix($message);
		return "{$prefix}[$level][$category] $text";
	}
}

Data precautions

  • Avoid passing resources and objects that can not be serialized in the array or rewrite formatMessage to handle that, e.g. trying to serialize PDO instance will raise fatal PHP error. Native Yii log targets will try to serialize that. One can check http://github.com/raven/raven to see how it sanitizes and handles these cases and use it's static methods.
  • Also check how raven can filter out private data such as password or sensitive financial requisites.

Inspired by

sheershoff/yii2-sentry-component 适用场景与选型建议

sheershoff/yii2-sentry-component 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.49k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2015 年 01 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「log」 「sentry」 「raven」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 sheershoff/yii2-sentry-component 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sheershoff/yii2-sentry-component 我们能提供哪些服务?
定制开发 / 二次开发

基于 sheershoff/yii2-sentry-component 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 14.49k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-01-30