diff --git a/README.md b/README.md index 2c9d034..91efa55 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,27 @@ Writing messages with extra data: ], 'category'); ``` +### Extra callback + +`extraCallback` property can modify extra's data as callable function + +```php + 'targets' => [ + [ + 'class' => 'notamedia\sentry\SentryTarget', + 'dsn' => 'http://2682ybvhbs347:235vvgy465346@sentry.com/1', + 'levels' => ['error', 'warning'], + 'context' => true // Write the context information. The default is true. + 'extraCallback' => function($context, $extra) { + // some manipulation with data + $extra['some_data'] = \Yii::$app->someComponent->someMethod(); + return $extra; + } + ], + ], +``` + + ## Log levels Yii2 log levels converts to Sentry levels: diff --git a/SentryTarget.php b/SentryTarget.php index 478ed26..c8feeac 100644 --- a/SentryTarget.php +++ b/SentryTarget.php @@ -12,7 +12,7 @@ /** * SentryTarget records log messages in a Sentry. - * + * * @see https://getsentry.com */ class SentryTarget extends Target @@ -29,11 +29,15 @@ class SentryTarget extends Target * @var bool Write the context information. The default implementation will dump user information, system variables, etc. */ public $context = true; + /** + * @var callable Callback function that can modify extra's array + */ + public $extraCallback; /** * @var \Raven_Client */ protected $client; - + /** * {@inheritdoc} */ @@ -42,7 +46,7 @@ public function collect($messages, $final) if (!isset($this->client)) { $this->client = new \Raven_Client($this->dsn, $this->clientOptions); } - + parent::collect($messages, $final); } @@ -78,6 +82,10 @@ public function export() $extra['context'] = parent::getContextMessage(); } + if (is_callable($this->extraCallback)) { + $extra = call_user_func($this->extraCallback, $context, $extra); + } + $data = [ 'level' => static::getLevelName($level), 'timestamp' => $timestamp, @@ -94,7 +102,7 @@ public function export() /** * Returns the text display of the specified level for the Sentry. - * + * * @param integer $level The message level, e.g. [[LEVEL_ERROR]], [[LEVEL_WARNING]]. * @return string */