Skip to content

Commit

Permalink
Merge branch 'master' of github.com:tylercd100/lern
Browse files Browse the repository at this point in the history
  • Loading branch information
tylercd100 committed Apr 15, 2016
2 parents aa62ad5 + f546962 commit 53791da
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to `LERN` will be documented in this file.

### 3.4.0
- Set context using a callback/closure (Thanks to [@qodeboy](https://github.com/qodeboy) for suggestion)

### 3.3.1
- Added default config values for Raven/Sentry

Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,35 @@ LERN::notify($e); //Notify the Exception

```

#### Changing the context

This function lets you change the context that is passed on to the Monolog handlers
```php
LERN::setContext(function(Exception $e, $context = []){

$context['exception'] = $e;

$app = app();

if(isset($app['auth']) && $user = $app['auth']->user())
{
if(empty($context['user']) or !is_array($context['user']))
{
$context['user'] = [];
}

if(!isset($context['user']['id']) && method_exists($user, 'getAuthIdentifier'))
{
$context['user']['id'] = $user->getAuthIdentifier();
}
}

return $context;
});

LERN::handle($exception);
```

## Further Reading and How-Tos
- [Creating relationships between Exceptions and Users](https://github.com/tylercd100/lern/wiki/Creating-relationships-between-exceptions-and-users)

Expand Down
50 changes: 34 additions & 16 deletions src/Components/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Notifier extends Component {
protected $log;
protected $messageCb;
protected $subjectCb;
protected $contextCb;

/**
* You can provide a Monolog Logger instance to use in the constructor
Expand Down Expand Up @@ -59,9 +60,9 @@ public function getMessage(Exception $e) {
if (is_callable($this->messageCb)) {
return $this->messageCb->__invoke($e);
} else {
$msg = get_class($e) . " was thrown! \n" . $e->getMessage();
$msg = get_class($e)." was thrown! \n".$e->getMessage();
if ($this->config['includeExceptionStackTrace'] === true) {
$msg .= "\n\n" . $e->getTraceAsString();
$msg .= "\n\n".$e->getTraceAsString();
}
return $msg;
}
Expand Down Expand Up @@ -90,6 +91,36 @@ public function getSubject(Exception $e) {
}
}

/**
* Set an array or a closure to be called that will generate the context array for the notification
* @param callable|array $cb A closure or array that will be set for the context
*/
public function setContext($cb)
{
$this->contextCb = $this->wrapValueInClosure($cb);
return $this;
}

/**
* Returns the result of the context closure
* @param Exception $e The Exception instance that you want to build the context around
* @return array The context array
*/
public function getContext(Exception $e, $context = []) {

//This needs a better solution. How do I specific context needs for different drivers?
if (in_array('pushover', $this->config['drivers'])) {
$context['sound'] = $this->config['pushover']['sound'];
}

// Call the callback or return the default
if (is_callable($this->contextCb)) {
return $this->contextCb->__invoke($e, $context);
} else {
return $context;
}
}

/**
* Pushes on another Monolog Handler
* @param HandlerInterface $handler The handler instance to add on
Expand All @@ -114,10 +145,9 @@ public function send(Exception $e, array $context = []) {

$message = $this->getMessage($e);
$subject = $this->getSubject($e);
$context = $this->getContext($e, $context);

try {
$context = $this->buildContext($context);

$notify = new Notify($this->config, $this->log, $subject);

$notify->critical($message, $context);
Expand All @@ -128,16 +158,4 @@ public function send(Exception $e, array $context = []) {
throw new NotifierFailedException($e->getMessage(), $code, $e);
}
}

/**
* Builds a context array to pass to Monolog
* @param array $context Additional information that you would like to pass to Monolog
* @return array The modified context array
*/
protected function buildContext(array $context = []) {
if (in_array('pushover', $this->config['drivers'])) {
$context['sound'] = $this->config['pushover']['sound'];
}
return $context;
}
}
8 changes: 4 additions & 4 deletions src/Components/Recorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected function collect($key, Exception $e = null) {
case 'data':
return $this->getData();
case 'status_code':
if($e===null) {
if ($e === null) {
return 0;
}
return $this->getStatusCode($e);
Expand Down Expand Up @@ -167,12 +167,12 @@ protected function getStatusCode(Exception $e) {
* @param array $data The array to remove keys from
* @return void
*/
protected function excludeKeys(array $data){
protected function excludeKeys(array $data) {
$keys = isset($this->config['excludeKeys']) ? $this->config['excludeKeys'] : [];
foreach ($data as $key => &$value) {
if(in_array($key,$keys)){
if (in_array($key, $keys)) {
unset($data[$key]);
} else if(is_array($value)){
} else if (is_array($value)) {
$value = $this->excludeKeys($value);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/LERNServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class LERNServiceProvider extends ServiceProvider
{
public function register() {
$this->mergeConfigFrom(__DIR__ . '/../config/lern.php', 'lern');
$this->mergeConfigFrom(__DIR__.'/../config/lern.php', 'lern');

$this->handleDeprecatedConfigValues();

Expand All @@ -19,9 +19,9 @@ public function register() {
public function boot()
{
$this->publishes([
__DIR__ . '/../migrations/2016_03_17_000000_create_lern_tables.php' => base_path('database/migrations/2016_03_17_000000_create_lern_tables.php'),
__DIR__ . '/../migrations/2016_03_27_000000_add_user_data_and_url_to_lern_tables.php' => base_path('database/migrations/2016_03_27_000000_add_user_data_and_url_to_lern_tables.php'),
__DIR__ . '/../config/lern.php' => base_path('config/lern.php'),
__DIR__.'/../migrations/2016_03_17_000000_create_lern_tables.php' => base_path('database/migrations/2016_03_17_000000_create_lern_tables.php'),
__DIR__.'/../migrations/2016_03_27_000000_add_user_data_and_url_to_lern_tables.php' => base_path('database/migrations/2016_03_27_000000_add_user_data_and_url_to_lern_tables.php'),
__DIR__.'/../config/lern.php' => base_path('config/lern.php'),
]);
}

Expand Down
6 changes: 6 additions & 0 deletions tests/NotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public function testNotifierReturnsTheCorrectMessageWhenUsingClosure(){
$this->assertEquals($result,"This is a test");
}

public function testNotifierReturnsTheCorrectContextWhenUsingClosure(){
$this->notifier->setContext(function($e,$context){return ["text"=>"This is a test"];});
$result = $this->notifier->getContext(new Exception);
$this->assertEquals($result,["text"=>"This is a test"]);
}

public function testNotifierReturnsTheCorrectMessageWhenUsingString(){
$this->notifier->setMessage("This is a test");
$result = $this->notifier->getMessage(new Exception);
Expand Down

0 comments on commit 53791da

Please sign in to comment.