Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

callback for context value #1

Merged
merged 6 commits into from
Apr 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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