Skip to content

Commit

Permalink
Merge pull request #1 from lithium-li/context_callback
Browse files Browse the repository at this point in the history
callback for context value
  • Loading branch information
niksamokhvalov committed Apr 8, 2016
2 parents c41de5e + 36d6f39 commit 655b09b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 12 additions & 4 deletions SentryTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

/**
* SentryTarget records log messages in a Sentry.
*
*
* @see https://getsentry.com
*/
class SentryTarget extends Target
Expand All @@ -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}
*/
Expand All @@ -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);
}

Expand Down Expand Up @@ -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,
Expand All @@ -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
*/
Expand Down

0 comments on commit 655b09b

Please sign in to comment.